[prev in list] [next in list] [prev in thread] [next in thread]
List: python-ideas
Subject: Re: [Python-ideas] getitem() (was Re: [Python-Dev] PEP 463: Exception-catching expressions)
From: Chris Angelico <rosuav () gmail ! com>
Date: 2014-02-22 5:38:54
Message-ID: CAPTjJmpX9ODJGGzEbRt118sTvFDhmsefjmKUi7od+pREsLd+ww () mail ! gmail ! com
[Download RAW message or body]
On Sat, Feb 22, 2014 at 4:14 PM, Eric Snow <ericsnowcurrently@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
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic