Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'try:': 0.07; '-1,': 0.09; 'exception,': 0.09; 'exception:': 0.09; 'def': 0.10; 'cls': 0.16; 'invalid.': 0.16; 'subject:arithmetic': 0.16; 'valueerror,': 0.16; 'default,': 0.22; 'raise': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'wondering': 0.26; '>>>>': 0.29; 'prints': 0.29; 'workaround': 0.29; 'print': 0.32; 'to:addr :python-list': 0.33; 'except': 0.36; 'but': 0.36; 'skip:z 10': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'here': 0.65; 'received:204': 0.72 Date: Thu, 23 Aug 2012 13:01:47 +0200 From: Laszlo Nagy User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Guarding arithmetic References: <8b9a5844-66b0-4940-946a-5e626462cdce@googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345719712 news.xs4all.nl 6900 [2001:888:2000:d::a6]:34350 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27722 >>>> def safe(deferred, default=42, exception=Exception): > ... try: > ... return deferred() > ... except exception: > ... return default What a beautiful solution! I was wondering if the following would be possible: def test(thing, default, *exc_classes): try: thing() except *exc_classes: return default But it is syntactically invalid. Here is a workaround that is not so beautiful: def test(thing, default, *exc_classes): try: thing() except Exception, e: for cls in exc_classes: if isinstance(e,cls): return default raise print test( (lambda: 1/0), -1, ValueError, ZeroDivisionError) # prints -1