[Python-ideas] getitem() (was Re: [Python-Dev] PEP 463: Exception-catching expressions)
Chris Angelico
rosuav at gmail.com
Sat Feb 22 06:38:54 CET 2014
On Sat, Feb 22, 2014 at 4:14 PM, Eric Snow <ericsnowcurrently at gmail.com> wrote:
> def getitem(seq, index, default=_NOT_SET):
> try:
> return seq[index]
> except IndexError:
> if default is _NOT_SET:
> raise
> return default
>
> Of course, one might expect getitem() to work for mappings too (or
> we'd have 2 distinct functions for mappings and sequences). Having
> getitem() work for both would not be too hard.
Very easy. Just catch LookupError or (IndexError, KeyError) instead of
IndexError.
ChrisA
More information about the Python-ideas
mailing list