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


Groups > comp.lang.python > #27758

Re: Guarding arithmetic

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; 'arguments': 0.07; 'except:': 0.07; 'try:': 0.07; 'python': 0.09; '*args):': 0.09; 'block.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terms,': 0.09; 'def': 0.10; 'aug': 0.13; 'defer': 0.16; 'lambda': 0.16; 'provision': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:arithmetic': 0.16; 'try/except': 0.16; 'thu,': 0.17; '>>>': 0.18; 'all,': 0.21; 'raise': 0.24; 'plain': 0.27; 'header:X -Complaints-To:1': 0.28; 'prints': 0.29; 'wrap': 0.29; 'call.': 0.30; 'function': 0.30; 'print': 0.32; 'getting': 0.33; 'function.': 0.33; 'url:home': 0.33; 'to:addr:python-list': 0.33; 'operations': 0.33; "can't": 0.34; 'received:org': 0.36; 'charset :us-ascii': 0.36; 'does': 0.37; 'subject:: ': 0.38; 'mark': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'called...': 0.84; 'dennis': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: Guarding arithmetic
Date Thu, 23 Aug 2012 14:49:53 -0400
Organization > Bestiaria Support Staff <
References <8b9a5844-66b0-4940-946a-5e626462cdce@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host adsl-76-249-24-126.dsl.klmzmi.sbcglobal.net
X-Newsreader Forte Agent 3.3/32.846
X-No-Archive YES
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3727.1345747783.4697.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1345747783 news.xs4all.nl 6909 [2001:888:2000:d::a6]:32857
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:27758

Show key headers only | View raw


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 | 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