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


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

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

Started byTerry Reedy <tjreedy@udel.edu>
First post2011-05-26 23:00 -0400
Last post2011-05-27 09:03 -0400
Articles 3 — 3 participants

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: bug in str.startswith() and str.endswith() Terry Reedy <tjreedy@udel.edu> - 2011-05-26 23:00 -0400
    Re: bug in str.startswith() and str.endswith() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-27 03:27 +0000
    Re: bug in str.startswith() and str.endswith() Mel <mwilson@the-wire.com> - 2011-05-27 09:03 -0400

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

FromTerry Reedy <tjreedy@udel.edu>
Date2011-05-26 23:00 -0400
SubjectRe: bug in str.startswith() and str.endswith()
Message-ID<mailman.2141.1306465246.9059.python-list@python.org>
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

[toc] | [next] | [standalone]


#6366

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-05-27 03:27 +0000
Message-ID<4ddf1a3c$0$29996$c3e8da3$5496439d@news.astraweb.com>
In reply to#6363
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

[toc] | [prev] | [next] | [standalone]


#6385

FromMel <mwilson@the-wire.com>
Date2011-05-27 09:03 -0400
Message-ID<iro7fs$9ep$1@speranza.aioe.org>
In reply to#6363
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.

[toc] | [prev] | [standalone]


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


csiph-web