FAQ
I've tried this in 2.5 - 3.2:

--> 'this is a test'.startswith('this')
True
--> 'this is a test'.startswith('this', None, None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: slice indices must be integers or None or have an __index__
method

The 3.2 docs say this:

str.startswith(prefix[, start[, end]])
Return True if string starts with the prefix, otherwise return False.
prefix can also be a tuple of prefixes to look for. With optional start,
test string beginning at that position. With optional end, stop
comparing string at that position

str.endswith(suffix[, start[, end]])
Return True if the string ends with the specified suffix, otherwise
return False. suffix can also be a tuple of suffixes to look for. With
optional start, test beginning at that position. With optional end, stop
comparing at that position.

Any reason this is not a bug?

~Ethan~

Search Discussions

  • MRAB at May 26, 2011 at 11:27 pm

    On 27/05/2011 00:27, Ethan Furman wrote:
    I've tried this in 2.5 - 3.2:

    --> 'this is a test'.startswith('this')
    True
    --> 'this is a test'.startswith('this', None, None)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: slice indices must be integers or None or have an __index__
    method

    The 3.2 docs say this:

    str.startswith(prefix[, start[, end]])
    Return True if string starts with the prefix, otherwise return False.
    prefix can also be a tuple of prefixes to look for. With optional start,
    test string beginning at that position. With optional end, stop
    comparing string at that position

    str.endswith(suffix[, start[, end]])
    Return True if the string ends with the specified suffix, otherwise
    return False. suffix can also be a tuple of suffixes to look for. With
    optional start, test beginning at that position. With optional end, stop
    comparing at that position.

    Any reason this is not a bug?
    Let's see: 'start' and 'end' are optional, but aren't keyword
    arguments, and can't be None...

    I'd say bug.
  • Carl Banks at May 27, 2011 at 12:59 am

    On Thursday, May 26, 2011 4:27:22 PM UTC-7, MRAB wrote:
    On 27/05/2011 00:27, Ethan Furman wrote:
    I've tried this in 2.5 - 3.2:

    --> 'this is a test'.startswith('this')
    True
    --> 'this is a test'.startswith('this', None, None)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: slice indices must be integers or None or have an __index__
    method

    The 3.2 docs say this:

    str.startswith(prefix[, start[, end]])
    Return True if string starts with the prefix, otherwise return False.
    prefix can also be a tuple of prefixes to look for. With optional start,
    test string beginning at that position. With optional end, stop
    comparing string at that position

    str.endswith(suffix[, start[, end]])
    Return True if the string ends with the specified suffix, otherwise
    return False. suffix can also be a tuple of suffixes to look for. With
    optional start, test beginning at that position. With optional end, stop
    comparing at that position.

    Any reason this is not a bug?
    Let's see: 'start' and 'end' are optional, but aren't keyword
    arguments, and can't be None...

    I'd say bug.
    I also say bug. The end parameter looks pretty useless for .startswith() and is probably only present for consistency with other string search methods like .index(). Yet on .index() using None as an argument works as intended:
    "cbcd".index("c",None,None)

    So it's there for consistency, yet is not consistent.


    Carl Banks
  • Mel at May 27, 2011 at 1:07 am

    Ethan Furman wrote:

    I've tried this in 2.5 - 3.2:

    --> 'this is a test'.startswith('this')
    True
    --> 'this is a test'.startswith('this', None, None)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: slice indices must be integers or None or have an __index__
    method

    The 3.2 docs say this:

    str.startswith(prefix[, start[, end]])
    Return True if string starts with the prefix, otherwise return False.
    prefix can also be a tuple of prefixes to look for. With optional start,
    test string beginning at that position. With optional end, stop
    comparing string at that position

    str.endswith(suffix[, start[, end]])
    Return True if the string ends with the specified suffix, otherwise
    return False. suffix can also be a tuple of suffixes to look for. With
    optional start, test beginning at that position. With optional end, stop
    comparing at that position.

    Any reason this is not a bug?
    It's a wart at the very least. The same thing happened in Python2 with
    range and xrange; there seemed no way to explicitly pass "default"
    arguments.

    Mel.
  • Roy Smith at May 27, 2011 at 1:13 am
    In article <mailman.2136.1306451668.9059.python-list at python.org>,
    Ethan Furman wrote:
    --> 'this is a test'.startswith('this')
    True
    --> 'this is a test'.startswith('this', None, None)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: slice indices must be integers or None or have an __index__
    method [...]
    Any reason this is not a bug?
    +1 for it being a bug.
  • Stefan Behnel at May 27, 2011 at 4:47 am

    Roy Smith, 27.05.2011 03:13:
    Ethan Furman wrote:
    --> 'this is a test'.startswith('this')
    True
    --> 'this is a test'.startswith('this', None, None)
    Traceback (most recent call last):
    File "<stdin>", line 1, in<module>
    TypeError: slice indices must be integers or None or have an __index__
    method [...]
    Any reason this is not a bug?
    +1 for it being a bug.
    Meaning that the right thing to do at this point is to file a bug report.

    Stefan
  • Roy Smith at May 27, 2011 at 1:48 pm
    In article <mailman.2144.1306471679.9059.python-list at python.org>,
    Stefan Behnel wrote:
    Roy Smith, 27.05.2011 03:13:
    Ethan Furman wrote:
    --> 'this is a test'.startswith('this')
    True
    --> 'this is a test'.startswith('this', None, None)
    Traceback (most recent call last):
    File "<stdin>", line 1, in<module>
    TypeError: slice indices must be integers or None or have an __index__
    method [...]
    Any reason this is not a bug?
    +1 for it being a bug.
    Meaning that the right thing to do at this point is to file a bug report.
    And now we just need to figure out if it's a bug in the code or the
    documentation :-)
  • Terry Reedy at May 27, 2011 at 3:00 am

    On 5/26/2011 7:27 PM, Ethan Furman wrote:
    I've tried this in 2.5 - 3.2:

    --> 'this is a test'.startswith('this')
    True
    --> 'this is a test'.startswith('this', None, None)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: slice indices must be integers or None or have an __index__
    method

    The 3.2 docs say this:

    str.startswith(prefix[, start[, end]])
    Return True if string starts with the prefix, otherwise return False.
    prefix can also be a tuple of prefixes to look for. With optional start,
    test string beginning at that position. With optional end, stop
    comparing string at that position
    To me, that says pretty clearly that start and end have to be
    'positions', ie, ints or other index types. So I would say that the
    error message is a bug. I see so reason why one would want to use None
    rather that 0 for start or None rather than nothing for end.

    --
    Terry Jan Reedy
  • Steven D'Aprano at May 27, 2011 at 3:27 am
    On Thu, 26 May 2011 23:00:32 -0400, Terry Reedy wrote:

    [...]
    To me, that says pretty clearly that start and end have to be
    'positions', ie, ints or other index types. So I would say that the
    error message is a bug. I see so reason why one would want to use None
    rather that 0 for start or None rather than nothing for end.
    def my_string_function(source, x, y, z, start, end):
    if source.startswith(x+y+z, start, end):
    ...

    I want sensible defaults for start and end. What should I set them to?

    def my_string_function(source, x, y, z, start=0, end=None):
    if end is None:
    flag = source.startswith(x+y+z, start)
    else:
    flag = source.startswith(x+y+z, start, end)
    if flag:
    ...

    Yuck. Perhaps a better alternative is:

    def my_string_function(source, x, y, z, start=0, end=None):
    t = (start,) if end is None else (start, end)
    if source.startswith(x+y+z, *t):
    ...

    but that's still pretty icky.


    --
    Steven
  • Duncan Booth at May 27, 2011 at 10:31 am

    Carl Banks wrote:

    The end parameter looks pretty useless for
    .startswith() and is probably only present for consistency with other
    string search methods like .index().
    No, the end parameter could be useful if the slice ends up shorter than the
    prefix string:
    'abcd'.startswith('abc', 0, 2)
    False

    Likewise the start parameter for endswith.
  • Mel at May 27, 2011 at 1:03 pm

    Terry Reedy wrote:

    To me, that says pretty clearly that start and end have to be
    'positions', ie, ints or other index types. So I would say that the
    error message is a bug. I see so reason why one would want to use None
    rather that 0 for start or None rather than nothing for end.
    If you're trying to wrap a call to startswith in a function that "looks
    like" startswith, there's no easy way to pass in the information that your
    caller wants the default parameters. The case I ran into was

    def wrapped_range (start, stop=None, span=None):
    do_some_things()
    result = range (start, stop, span) # range doesn't(/didn't) accept this
    return result


    Tne answer in that case was to take *args as the parameter to wrapped_range
    and count arguments to distinguish between the different calls to range.

    Mel.
  • Ethan Furman at May 27, 2011 at 5:14 pm

    Ethan Furman wrote:
    Any reason this is not a bug?
    Looks like someone else beat me to filing:

    http://bugs.python.org/issue11828

    Looks like they fixed it as well.

    ~Ethan~

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 26, '11 at 11:27p
activeMay 27, '11 at 5:14p
posts12
users9
websitepython.org

People

Translate

site design / logo © 2022 Grokbase