Bug in string.find; was: Re: Proposed PEP: New style indexing, was Re: Bug in slice type
steve at holdenweb.com
Thu Aug 25 06:05:18 CEST 2005
Bryan Olson wrote:
> The doc for the find() method of string objects, which is
> essentially the same as the string.find() function, states:
> find(sub[, start[, end]])
> Return the lowest index in the string where substring sub
> is found, such that sub is contained in the range [start,
> end). Optional arguments start and end are interpreted as
> in slice notation. Return -1 if sub is not found.
> print 'Hello'.find('o')
> import string
> print string.find('Hello', 'o')
> The substring 'o' is found in 'Hello' at the index -1, and at
> the index 4, and it is not found at any other index. Both the
> locations found are in the range [start, end), and obviously -1
> is less than 4, so according to the documentation, find() should
> return -1.
> What the either of the above actually prints is:
> which shows yet another bug resulting from Python's handling of
> negative indexes. This one is clearly a documentation error, but
> the real fix is to cure the wart so that Python's behavior is
> consistent enough that we'll be able to describe it correctly.
Do you just go round looking for trouble?
As far as position reporting goes, it seems pretty clear that find()
will always report positive index values. In a five-character string
then -1 and 4 are effectively equivalent.
What on earth makes you call this a bug? And what are you proposing that
find() should return if the substring isn't found at all? please don't
suggest it should raise an exception, as index() exists to provide that
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
More information about the Python-list