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


Groups > comp.lang.python > #27711

Re: Guarding arithmetic

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder2.ecngs.de!ecngs!feeder.ecngs.de!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; 'argument': 0.04; 'exception.': 0.07; 'suppose': 0.07; 'try:': 0.07; 'python': 0.09; 'defined.': 0.09; 'here?': 0.09; 'def': 0.10; 'aug': 0.13; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'integer.': 0.16; 'nan': 0.16; 'possible?': 0.16; 'subject:arithmetic': 0.16; 'wrote:': 0.17; 'thu,': 0.17; 'define': 0.20; 'received:209.85.214.174': 0.21; 'raise': 0.24; 'header:In-Reply-To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'division': 0.29; 'prints': 0.29; "i'm": 0.29; 'function': 0.30; 'could': 0.32; 'print': 0.32; 'like:': 0.33; 'point,': 0.33; 'to:addr:python-list': 0.33; 'likely': 0.33; 'received:google.com': 0.34; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'except': 0.36; 'but': 0.36; 'possible': 0.37; 'why': 0.37; 'passed': 0.37; 'skip:z 10': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'mark': 0.38; 'easier': 0.38; 'sure': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'most': 0.61; '666': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=q+EgosQmQsjs0CDE6uv/nWWd9U82e2QcbJTnbZCVfaY=; b=Db6FCw9RmtQXk8JFxGPO32e7TPN6iGwaEKbOClMZOwt7QdyQpyI3sWW9ICzf+ZqQj0 5zJ7NJ2+qoCz4k3j7ll2YM//FuyYbHs0hx+QaLsGDmgP6cN89RkJkzOxDp7Q4g54ukba yOvNQGAz2YTa9ALQKmrZgoUJkjpvmxnKc8nLBSFy9rl1Sbu58e12jtIVqhjycSGevPW5 STEZLuTnYz49GO77xfuUEecZeArbwss17kGSQCZXfBEtzUizKeCVni2irQyFTxHZvPTo X12TrMGIITiGLK7ZAURooc/uWoyh2Ihd7UdqyWlFy2SmDOmRfLVaLsvc2+0DZAuZmy/a 9NyQ==
MIME-Version 1.0
In-Reply-To <8b9a5844-66b0-4940-946a-5e626462cdce@googlegroups.com>
References <8b9a5844-66b0-4940-946a-5e626462cdce@googlegroups.com>
Date Thu, 23 Aug 2012 19:16:08 +1000
Subject Re: Guarding arithmetic
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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.3700.1345713371.4697.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1345713371 news.xs4all.nl 6926 [2001:888:2000:d::a6]:41379
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:27711

Show key headers only | View raw


On Thu, Aug 23, 2012 at 7:05 PM, Mark Carter <alt.mcarter@gmail.com> wrote:
> Suppose I want to define a function "safe", which returns the argument passed if there is no error, and 42 if there is one. So the setup is something like:
>
> def safe(x):
>    # WHAT WOULD DEFINE HERE?
>
> print safe(666) # prints 666
> print safe(1/0) # prints 42
>
> I don't see how such a function could be defined. Is it possible?

That can work ONLY if the division of 1/0 doesn't raise an exception.
This is why the concept of NaN exists; I'm not sure if there's a way
to tell Python to return NaN instead of bombing, but it's most likely
only possible with floating point, not integer.

However, there's an easier way.

try:
    print 1/0
except ZeroDivisionError:
    print 42

Catch the exception and do with it what you will.

ChrisA

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