This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author remi.lapeyre
Recipients marcospb19, remi.lapeyre
Date 2020-05-06.15:28:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1588778923.4.0.0865144370071.issue40531@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2020-05-06 15:28:43remi.lapeyresetrecipients: + remi.lapeyre, marcospb19
2020-05-06 15:28:43remi.lapeyresetmessageid: <1588778923.4.0.0865144370071.issue40531@roundup.psfhosted.org>
2020-05-06 15:28:43remi.lapeyrelinkissue40531 messages
2020-05-06 15:28:43remi.lapeyrecreate