Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #6355 > unrolled thread

Re: bug in str.startswith() and str.endswith()

Started byCarl Banks <pavlovevidence@gmail.com>
First post2011-05-26 17:59 -0700
Last post2011-05-27 10:31 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Re: bug in str.startswith() and str.endswith() Carl Banks <pavlovevidence@gmail.com> - 2011-05-26 17:59 -0700
    Re: bug in str.startswith() and str.endswith() Duncan Booth <duncan.booth@invalid.invalid> - 2011-05-27 10:31 +0000

#6355 — Re: bug in str.startswith() and str.endswith()

FromCarl Banks <pavlovevidence@gmail.com>
Date2011-05-26 17:59 -0700
SubjectRe: bug in str.startswith() and str.endswith()
Message-ID<mailman.2139.1306457951.9059.python-list@python.org>
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)
0

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


Carl Banks

[toc] | [next] | [standalone]


#6382

FromDuncan Booth <duncan.booth@invalid.invalid>
Date2011-05-27 10:31 +0000
Message-ID<Xns9EF2753877752duncanbooth@127.0.0.1>
In reply to#6355
Carl Banks <pavlovevidence@gmail.com> 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.

-- 
Duncan Booth http://kupuguy.blogspot.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web