[Python-ideas] Deprecate str.find

Ethan Furman ethan at stoneleaf.us
Sat Jul 16 17:35:10 CEST 2011


Nick Coghlan wrote:
> So, I have a different suggestion (that actually paves the way for the
> eventual deprecation of find()):
> 
>     Update str.index() to accept an optional sentinel value.
> 
> If the sentinel argument is not supplied, then a missing substring
> raises ValueError as it does now. If it is supplied, then a missing
> substring returns the sentinel value instead of throwing an exception.
> The above idiom could then be expressed cleanly as:
> 
> idx = s.index(sub, missing=None)
> if idx is not None:
>    # do something that needs idx

+1

Excellent idea.


> However, this seemingly simple suggestion is complicated by the fact
> that string methods do not currently accept keyword arguments and
> index() already accepts two optional positional arguments (for
> substring searching).

Does this require a PEP or just someone to start working on it?  Getting 
str() to accept keyword arguments and adding missing to str.index() 
sounds like a nice project.

~Ethan~



More information about the Python-ideas mailing list