Message368267
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. |
|
Date |
User |
Action |
Args |
2020-05-06 15:28:43 | remi.lapeyre | set | recipients:
+ remi.lapeyre, marcospb19 |
2020-05-06 15:28:43 | remi.lapeyre | set | messageid: <1588778923.4.0.0865144370071.issue40531@roundup.psfhosted.org> |
2020-05-06 15:28:43 | remi.lapeyre | link | issue40531 messages |
2020-05-06 15:28:43 | remi.lapeyre | create | |
|