[Python-ideas] Deprecate str.find

Devin Jeanpierre jeanpierreda at gmail.com
Sun Jul 17 00:15:51 CEST 2011


On Sat, Jul 16, 2011 at 6:05 PM, Raymond Hettinger
<raymond.hettinger at gmail.com> wrote:
>
> On Jul 16, 2011, at 2:04 PM, Mike Graham wrote:
>>
>> Did you catch Nick's suggestion to enhance str.index to have the same
>> basic API as dict.get/getattr/etc.?
>
> Sorry, but I thought idea that was way off base.
> Things like dict.get and getattr are about returning values,
> so it is possible to provide a meaningful default.
> In the case of string.find, there is no meaningful
> default position in the string.  So, a default would simply
> be a way to turn the -1 value into some other object
> which you would still need to test.

Slicing was brought up as a use-case where you can use a default
without checking.

mystring[:mystring.index('#', None)], for example, could conceivably
be used somewhere to strip comments from (some sort of) code, for
example.

It does have other benefits too. It makes the return value on failure
explicit, which would help remind people to check, or be more
immediately aware when reading code. And it does have nice parallels
to those other methods.

Devin



More information about the Python-ideas mailing list