Sujet : Re: Two python issues
De : jsf80238 (at) *nospam* gmail.com (Jason Friedman)
Groupes : comp.lang.pythonDate : 05. Nov 2024, 22:08:45
Autres entêtes
Message-ID : <mailman.83.1730840940.4695.python-list@python.org>
References : 1 2
>
(a) An error-prone "feature" is returning -1 if a substring is not found
by "find", since -1 currently refers to the last item. An example:
>
>>> s = 'qwertyuiop'
>>> s[s.find('r')]
'r'
>>> s[s.find('p')]
'p'
>>> s[s.find('a')]
'p'
>>>
>
If "find" is unsuccessful, an error message is the only clean option.
Moreover, using index -1 for the last item is a bad choice: it should be
len(s) - 1 (no laziness!).
>
I'm not sure if this answers your objection but the note in the
documentation (
https://docs.python.org/3/library/stdtypes.html#str.find)
says:
The find() method should be used only if you need to know the position of
sub.
I think the use case above is a little bit different.