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


Groups > comp.lang.python > #27723

Re: Guarding arithmetic

Date 2012-08-23 12:21 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: Guarding arithmetic
References <8b9a5844-66b0-4940-946a-5e626462cdce@googlegroups.com> <k14vjv$ln7$1@ger.gmane.org> <50360D9B.30303@shopzeus.com>
Newsgroups comp.lang.python
Message-ID <mailman.3709.1345720887.4697.python-list@python.org> (permalink)

Show all headers | View raw


On 23/08/2012 12:01, Laszlo Nagy wrote:
>
>>>>> 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
>
The 'except' clause accepts a tuple of exception classes, so this works:

def test(thing, default, *exc_classes):
     try:
         thing()
     except exc_classes:
         return default

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


Thread

Guarding arithmetic Mark Carter <alt.mcarter@gmail.com> - 2012-08-23 02:05 -0700
  Re: Guarding arithmetic Chris Angelico <rosuav@gmail.com> - 2012-08-23 19:16 +1000
    Re: Guarding arithmetic Mark Carter <alt.mcarter@gmail.com> - 2012-08-23 02:22 -0700
    Re: Guarding arithmetic Chris Angelico <rosuav@gmail.com> - 2012-08-23 19:29 +1000
    Re: Guarding arithmetic Mark Carter <alt.mcarter@gmail.com> - 2012-08-23 02:22 -0700
  Re: Guarding arithmetic Laszlo Nagy <gandalf@shopzeus.com> - 2012-08-23 11:23 +0200
    Re: Guarding arithmetic Mark Carter <alt.mcarter@gmail.com> - 2012-08-23 02:47 -0700
    Re: Guarding arithmetic Mark Carter <alt.mcarter@gmail.com> - 2012-08-23 02:47 -0700
  Re: Guarding arithmetic Laszlo Nagy <gandalf@shopzeus.com> - 2012-08-23 11:28 +0200
  Re: Guarding arithmetic Chris Angelico <rosuav@gmail.com> - 2012-08-23 19:30 +1000
  Re: Guarding arithmetic Peter Otten <__peter__@web.de> - 2012-08-23 12:11 +0200
    Re: Guarding arithmetic rusi <rustompmody@gmail.com> - 2012-08-23 10:15 -0700
  Re: Guarding arithmetic Laszlo Nagy <gandalf@shopzeus.com> - 2012-08-23 13:01 +0200
  Re: Guarding arithmetic MRAB <python@mrabarnett.plus.com> - 2012-08-23 12:21 +0100
  Re: Guarding arithmetic Peter Otten <__peter__@web.de> - 2012-08-23 13:28 +0200
  Re: Guarding arithmetic Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-23 15:11 +0100
  Re: Guarding arithmetic Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-23 14:49 -0400
  Re: Guarding arithmetic Chris Angelico <rosuav@gmail.com> - 2012-08-24 07:48 +1000

csiph-web