On Thu, Aug 27, 2020 at 3:37 PM M.-A. Lemburg <m...@egenix.com> wrote:

> If I'm not mistaken, all those listed cases fall under the second case
> I mentioned (branching on length).
>
> If your API allows for optional list entries, then it's better to
> branch on length (e.g. as in the pytest case). If this happens only in
> exceptional cases, use try-except (most other cases you listed).
>

All but the first example, i.e. all the uses of try-except, were found by
grepping for `except IndexError`. That was just the easiest method I found
to find examples. The fact that try-except was used in those cases does not
necessarily mean that the index is missing "only in exceptional cases".
Perhaps because you like to emphasise intent when you're coding you assume
that "exceptional" is the intent when you see the keyword "except". It's
not how I interpret these examples. I think truly exceptional cases would
have a change in behaviour like showing a warning. All that's happening
here is getting a default value, and using try-except is the easiest
available syntax. Often the use case is that the list may be empty or
you're at the edge of the list and you need to go one beyond. That's normal
and expected, not exceptional. If list.get existed then probably it would
have been used in many of these examples. But of course it didn't exist and
I don't have a way of searching for custom get functions so nearly all the
examples use except which you take to mean exceptional.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/FPQUJVFKXEEBZBHK5GBCEKPU7IJIZZCS/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to