Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106180

Re: Slice equivalent to dict.get

From Zachary Ware <zachary.ware+pylist@gmail.com>
Newsgroups comp.lang.python
Subject Re: Slice equivalent to dict.get
Date 2016-03-31 14:28 -0500
Message-ID <mailman.277.1459452550.28225.python-list@python.org> (permalink)
References <56fd3d17$0$1606$c3e8da3$5496439d@news.astraweb.com> <ndjfg3$b3k$1@ger.gmane.org> <ndjo31$rs4$1@ger.gmane.org>

Show all headers | View raw


On Thu, Mar 31, 2016 at 12:51 PM, Terry Reedy <tjreedy@udel.edu> wrote:
> On 3/31/2016 11:24 AM, Peter Otten wrote:
>> try...except to the rescue:
>>
>>>>> def get(seq, index, default=None):
>>
>> ...     try: return seq[index]
>> ...     except IndexError: return default
>
>
> Replace IndexError with (IndexError, KeyError) and the functions works with
> any subscriptable that raises either exception.

Or use the superclass of both of those, LookupError, and also catch
anything that uses that more generic name as well.

-- 
Zach

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Slice equivalent to dict.get Steven D'Aprano <steve@pearwood.info> - 2016-04-01 02:07 +1100
  Re: Slice equivalent to dict.get Peter Otten <__peter__@web.de> - 2016-03-31 17:24 +0200
  Re: Slice equivalent to dict.get Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-31 09:43 -0600
  Re: Slice equivalent to dict.get "Sven R. Kunze" <srkunze@mail.de> - 2016-03-31 18:05 +0200
  Re: Slice equivalent to dict.get Terry Reedy <tjreedy@udel.edu> - 2016-03-31 13:51 -0400
  Re: Slice equivalent to dict.get Zachary Ware <zachary.ware+pylist@gmail.com> - 2016-03-31 14:28 -0500

csiph-web