S
Steve Holden
If you want an exception from your code when 'w' isn't in the string youBryan 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"
What I don't understand is why you want it to return something that
isn't a legal index.
In this case, so that errors are caught as close to their
occurrence as possible. I see no good reason for the following
to happily print 'y'.
s = 'buggy'
print s[s.find('w')]
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.
The bug that got me was what a slice object reports as the
'stop' bound when the step is negative and the slice includes
index 0. Took me hours to figure out why my code was failing.
The double-meaning of -1, as both an exclusive stopping bound
and an alias for the highest valid index, is just plain whacked.
Unfortunately, as negative indexes are currently handled, there
is no it-just-works value that slice could return.
should consider using index() rather than find.
Otherwise, whatever find() returns you will have to have an "if" in
there to handle the not-found case.
This just sounds like whining to me. If you want to catch errors, use a
function that will raise an exception rather than relying on the
invalidity of the result.
regards
Steve