[Python-Dev] Let's stop eating exceptions in dict lookup

Tim Peters tim.peters at gmail.com
Tue May 30 06:10:08 CEST 2006


[Greg Ewing]
>> Although Tim pointed out that replace() only regards
>> n+1 empty strings as existing in a string of lenth
>> n. So for consistency, find() should only find them
>> in those places, too.

[Guido]
> And "abc".count("") should return 4.

And it does, but too much context was missing in Greg's reply to make
sense of his comment.  In context Greg was seeming to say that

    "abc".count("", 100)

should return 0, and

    "abc".find("", 100)

should return -1, since "the only" empty substrings of "abc" are at
slices 0:0, 1:1, 2:2 and 3:3.  In fact we have

>>> "abc".count("", 100)
1
>>> u"abc".count("", 100)
1
>>> "abc".find("", 100)
100
>>> u"abc".find("", 100)
100

today, although the idea that find() can return an index that doesn't
exist in the string is particularly jarring.  Since we also have:

>>> "abc".rfind("", 100)
3
>>> u"abc".rfind("", 100)
3

it's twice as jarring that "searching from the right" finds the target
at a _smaller_ index than "searching from the left".


More information about the Python-Dev mailing list