Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27758
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Subject | Re: Guarding arithmetic |
| Date | 2012-08-23 14:49 -0400 |
| Organization | > Bestiaria Support Staff < |
| References | <8b9a5844-66b0-4940-946a-5e626462cdce@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3727.1345747783.4697.python-list@python.org> (permalink) |
On Thu, 23 Aug 2012 02:05:50 -0700 (PDT), Mark Carter
<alt.mcarter@gmail.com> declaimed the following in
gmane.comp.python.general:
> print safe(1/0) # prints 42
>
1/0 is evaluated before safe() is called... and hence will raise an
exception before getting into the function.
Python does not defer operations that are arguments of a function
call. You can only defer them by wrapping them into a function...
>>> def safe(arg):
... try:
... return arg()
... except:
... return 42
...
>>> print safe(lambda : 1/0)
42
Unfortunately, that means you can't just take an arbitrary equation
and wrap it in a call to safe() as you have to make provision for local
names...
>>> def safe(fnc, *args):
... try:
... return fnc(*args)
... except:
... return 42
...
>>> print safe(lambda a, b : a / b, 1, 0)
42
>>> print safe(lambda a, b : a / b, 1, 1)
1
>>>
>>> a = 1
>>> b = 0
>>> print safe(lambda a, b : a / b, a, b)
42
>>> b = 2
>>> print safe(lambda a, b : a / b, a, b)
0
>>>
By the time you wrap the equation with a lambda all, named, terms,
AND supply the named terms after the lambda, you might as well just wrap
the equation in a plain try/except block.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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