Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Zachary Ware Newsgroups: comp.lang.python Subject: Re: Slice equivalent to dict.get Date: Thu, 31 Mar 2016 14:28:47 -0500 Lines: 18 Sender: zachary.ware@gmail.com Message-ID: References: <56fd3d17$0$1606$c3e8da3$5496439d@news.astraweb.com> Reply-To: python-list@python.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de p7Lw1eP9P3DyXCibDQcehQdV9s/7+DobGLmyNbdY/MhA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'exception.': 0.07; 'raises': 0.07; 'received:209.85.218': 0.10; 'def': 0.13; 'thu,': 0.15; '2016': 0.16; 'indexerror:': 0.16; 'received:209.85.218.51': 0.16; 'received:io': 0.16; 'received:mail-oi0-f51.google.com': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'those,': 0.16; 'wrote:': 0.16; 'sender:addr:gmail.com': 0.18; 'try:': 0.18; 'to:name:python-list@python.org': 0.20; '31,': 0.22; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'index,': 0.29; 'except': 0.34; 'received:google.com': 0.35; 'generic': 0.35; 'replace': 0.35; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'anything': 0.38; 'well.': 0.40; 'to:addr:python.org': 0.40; 'default': 0.61; 'more': 0.63; 'mar': 0.65; '>>>>>': 0.66; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'otten': 0.84; 'reply-to:addr:python.org': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:reply-to:sender:in-reply-to:references:from:date :message-id:subject:to; bh=0EQ6S3zmTRqY20d1/kz7aJprOoVjTkz8xbeB0sO+3ls=; b=0I3I+srUZutJU4CxJUsHnzjxbXAXWBQpUhQIV3Puv/A610fjmac2ly97xbHLCYnZ+V DRb37iM34jpm8Zf/6QDlkY4TBPgz3nLZEFe/vLqckgWR+h2tzd0Ac5oJasEzDu9C5wpa 1gHIkfageC+vXmwl2JNYEBtSYR3tGN8cgVbr+nX0dLTPQVzME3X7sGoISjPfy0ya/pLv j70T9e8+Jf25+qjzLVhEZWKG900M9R5oLcXkc+HLjWkbC/O8wRAGCujMGsUaHVzarwlO ihSToq/wq71CKDr8Y6aFf1DTxrkAlAIhXRF2gR4cq1IxJ1lkzlh5nAXeWDnVwzfFLTrX KavQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:reply-to:sender:in-reply-to :references:from:date:message-id:subject:to; bh=0EQ6S3zmTRqY20d1/kz7aJprOoVjTkz8xbeB0sO+3ls=; b=BbOAoHqblbLtswKBLyQ5GfMJU706pQgjrp/Cuu4DZEOa4jJx/rFLtIT8+ObZqY6QMv HQGxyAUkHXTTBpAKfoJ31n/xwbbCmOadEVdxuli5WmrxChSmTuaqC5qpfqh5eFra20KV x+i1Rb05ZagRhcxxVeu4XanaClt+B5fFn1QufNMofKvhcvLAv0khyymn4jHhV73993Ba OkBhBMEWuMRtlX7TLVQZJZUMUmn9xHg8ZgVS69m3T+GNUmzNPbKeG/x0BhSQf99CtT7t entyVuvVxwOUPIylFTxp0804S7QxIUUR0WjcYUhePwJaUO3TBGrnU6NmtYK2NZjsWgiS wCxg== X-Gm-Message-State: AD7BkJJkczjoBb7/9F4q9M3jfNauCrimqpyWtX7EtkSinalibz1aK9Wpq8VamnWOJ6GFJf1MSrrvkyVbZlYeag== X-Received: by 10.157.0.10 with SMTP id 10mr468429ota.194.1459452547006; Thu, 31 Mar 2016 12:29:07 -0700 (PDT) In-Reply-To: X-Google-Sender-Auth: oUFr-1Uke4Mub2hZA_M9bY4bTFk X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:106180 On Thu, Mar 31, 2016 at 12:51 PM, Terry Reedy 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