[Python-ideas] Deprecate str.find
Mike Graham
mikegraham at gmail.com
Sat Jul 16 23:04:58 CEST 2011
On Sat, Jul 16, 2011 at 4:37 PM, Raymond Hettinger
<raymond.hettinger at gmail.com> wrote:
> On Jul 16, 2011, at 12:26 PM, Mike Graham wrote:
>> Using sub in s instead of
>> s.find(sub) != -1 is *already* the advice in the official Python
>> documentation,
>
> I know about that advice. I believe I'm the one who added it ;-)
> (as well as many other tips in the code modernization PEP).
Then it seems you're guiltier than I of saying what way other people
should write their programs. =)
On Sat, Jul 16, 2011 at 4:52 PM, Raymond Hettinger
<raymond.hettinger at gmail.com> wrote:
> I just remembered one other thought on the subject.
>
> Usually, when Python introduces a method such as .index()
> that raises an exception for the not-found case, there
> are immediate requests for variants that don't raise exceptions:
>
> dict.pop(key, default)
> dict.get(key, defalut)
> next(iterable, default)
> getattr(obj, attr, default)
> re.match() --> None or matchobject
>
> People seem to hate wrapping try/except around simple calls.
> Expect those people to be agitated if you take away str.find().
>
>
> Raymond
Did you catch Nick's suggestion to enhance str.index to have the same
basic API as dict.get/getattr/etc.? I think this might be useful
overall by providing a way to get a useful default, which is very
often None (as a value in a slice).
Mike
More information about the Python-ideas
mailing list