B
Bryan Olson
Robert said:> By "+1" he means, "I like it." He's not correcting you.
Ah, O.K. Thanks.
Robert said:> By "+1" he means, "I like it." He's not correcting you.
Do you just go round looking for trouble?Bryan said: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.
Consider:
print 'Hello'.find('o')
or:
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:
4
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.
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
functionality.
Steve Holden said: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 functionality.
> 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
> functionality.
Op 2005-08-25 said:Steve Holden asked:
In the course of programming, yes, absolutly.
What you just said, versus what the doc says.
There are a number of good options. A legal index is not one of
them.
Antoon said:> Bryan Olson schreef:
>
>
> IMO, with find a number of "features" of python come together.
> that create an awkward situation.
>
> 1) 0 is a false value, but indexes start at 0 so you can't
> return 0 to indicate nothing was found.
>
> 2) -1 is returned, which is both a true value and a legal
> index.
>
> It probably is too late now, but I always felt, find should
> have returned None when the substring isn't found.
Steve Holden asked:
In the course of programming, yes, absolutly.
What you just said, versus what the doc says.
There are a number of good options. A legal index is not one of
them.
We might agree, before further discussion, that this isn't the mostBryan said:None is certainly a reasonable candidate. The one-past-the-end
value, len(sequence), would be fine, and follows the preferred
idiom of C/C++. I don't see any elegant way to arrange for
successful finds always to return a true value and unsuccessful
calls to return a false value.
The really broken part is that unsuccessful searches return a
legal index.
What I don't understand is why you want it to return something thatMy suggestion doesn't change what find() returns, and doesn't
break code. Negative one is a reasonable choice to represent an
unsuccessful search -- provided it is not a legal index. Instead
of changing what find() returns, we should heal the
special-case-when-index-is-negative-in-a-certain-range wart.
Steve said:> We might agree, before further discussion, that this isn't the most>> Antoon Pardon wrote:>>>> > It probably is too late now, but I always felt, find should
>> > have returned None when the substring isn't found.
>> None is certainly a reasonable candidate. [...]
>> The really broken part is that unsuccessful searches return a
>> legal index.
>>
> elegant part of Python's design, and it's down to history that this tiny
> little wart remains.
> What I don't understand is why you want it to return something that
> isn't a legal index.
> Before using the result you always have to perform
> a test to discriminate between the found and not found cases. So I don't
> really see why this wart has put such a bug up your ass.
Bryan said:Steve said:We might agree, before further discussion, that this isn't the mostBryan said:Antoon Pardon wrote:
It probably is too late now, but I always felt, find should
have returned None when the substring isn't found.
None is certainly a reasonable candidate. [...]
The really broken part is that unsuccessful searches return a
legal index.
elegant part of Python's design, and it's down to history that this tiny
little wart remains.
I don't think my proposal breaks historic Python code, and I
don't think it has the same kind of unfortunate subtle
consequences as the current indexing scheme. You may think the
wart is tiny, but the duct-tape* is available so let's cure it.
[*] http://www.google.com/search?as_q=warts+"duct+tape"
Bryan Olson said:The double-meaning of -1, as both an exclusive stopping bound
and an alias for the highest valid index, is just plain whacked.
Terry Reedy said:I agree in this sense: the use of any int as an error return is an
unPythonic *nix-Cism, which I believe was copied therefrom. Str.find is
redundant with the Pythonic exception-raising str.index and I think it
should be removed in Py3.
Paul Rubin said:I like having it available so you don't have to clutter your code with
try/except if the substring isn't there. But it should not return a
valid integer index.
Terry Reedy said:The try/except pattern is a pretty basic part of Python's design.
One could say the same about clutter for *every* function or
method that raises an exception on invalid input. Should more or
even all be duplicated? Why just this one?
Terry Reedy said:The try/except pattern is a pretty basic part of Python's design. One
could say the same about clutter for *every* function or method that raises
an exception on invalid input. Should more or even all be duplicated? Why
just this one?
Bryan said:The conclusion is inescapable: Python's handling of negative
subscripts is a wart. Indexing from the high end is too useful
to give up, but it should be specified by the slicing/indexing
operation, not by the value of the index expression.
PPEP (Proposed Python Enhancement Proposal): New-Style Indexing
Instead of:
sequence[start : stop : step]
new-style slicing uses the syntax:
sequence[start ; stop ; step]
Paul Rubin said:Someone must have thought str.find was worth having, or else it
wouldn't be in the library.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.