[Python-Dev] Search for empty substrings (was Re: Let's stop eating exceptions in dict lookup)

Tim Peters tim.peters at gmail.com
Thu Jun 1 03:20:02 CEST 2006

[Fredrik Lundh]
> would "abc".find("", 100) == 3 be okay?  or should we switch to treating the
> optional start and end positions as "return value boundaries" (used to filter the
> result) rather than "slice directives" (used to process the source string before
> the operation)?  it's all trivial to implement, and has no performance implications,
> but I'm not sure what the consensus really is...

FWIW, I like what you eventually did:

>>> "ab".find("")
>>> "ab".find("", 1)
>>> "ab".find("", 2)
>>> "ab".find("", 3)
>>> "ab".rfind("")
>>> "ab".rfind("", 1)
>>> "ab".rfind("", 2)
>>> "ab".rfind("", 3)

I don't know that a compelling argument can be made for such a
seemingly senseless operation, but the behavior above is at least
consistent with the rule that a string of length n has exactly n+1
empty substrings, at 0:0, 1:1, ..., and n:n.

More information about the Python-Dev mailing list