[issue40531] Adding the method find() to list
Rémi Lapeyre
report at bugs.python.org
Wed May 6 11:28:43 EDT 2020
Rémi Lapeyre <remi.lapeyre at henki.fr> added the comment:
Hi João, ideas like this can also be proposed first on the python-ideas mailing list but as you said in your post there is already a method to do this and it raises ValueError when it is not found which is a common idiom in Python. Other objects don't often have two versions of the same method, one that raises an exception on error and one that returns a sentinel, most only have the one that raises.
Notice that your example is not simpler with your proposal:
# Example driver code:
index = find(list, possible_element)
if index_of_element == -1:
pass # Not found
else:
pass # Found
is a hard to read than and actually longer:
try:
index = list.index(possible_element)
except ValueError:
index = -1
if you really care about the number of lines you could use, while I don't recommend it:
try: index = list.index(possible_element)
except ValueErrort: index = -1
Also adding a new method to list would mean adding it to all sequence objects and that is asking for a lot.
----------
nosy: +remi.lapeyre
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40531>
_______________________________________
More information about the Python-bugs-list
mailing list