Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; 'arguments': 0.07; 'try:': 0.07; 'it;': 0.09; 'macros': 0.09; 'def': 0.10; 'aug': 0.13; 'language': 0.14; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'specified.': 0.16; 'subject:arithmetic': 0.16; 'wrote:': 0.17; 'thu,': 0.17; 'error.': 0.21; 'received:209.85.214.174': 0.21; 'this:': 0.23; 'header:In-Reply-To:1': 0.25; 'looks': 0.26; 'guess': 0.27; 'handling': 0.27; 'prevent': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'wrap': 0.29; 'call.': 0.30; 'evaluation': 0.30; 'function': 0.30; 'asking': 0.32; 'could': 0.32; 'print': 0.32; 'problem': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'exist': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'except': 0.36; 'possible': 0.37; 'ok,': 0.37; 'skip:z 10': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'mark': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'more': 0.63 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=R0dL2iTEte2GxxtUnjuTxgqiM77PvzBstT2s+OZxPaM=; b=upF7spuUDUVe0mm+DTiT7YqfO3D6xIWFmFM8k+4vI5U8ZYR3QmdInxOyLnYlUZHWDS uMLG1W1HL1hAdC0XijNzhGQ0wCTAOsfp+ISdG+lyzlxS3eEG9uiwjavQclgERa5k7t+7 DHwheU09/ANlcAoahJyyhjLJyOPUgYOlRtz9vIUKi5eJOJpoyhJaY48YDAxfO7KnhikE jzTYTwl1ENlbZDvfB/uL9+rMi6jW6Iw3aQFiEMVEPufY/GgTcjZDo5R+QzuPw7chBwiU MUSoKLb7ohRiwsR1s7PchGrqmbtL5f69nDbyhpDkql2w72VQKxT0+sLW1PYPvFoL/8pD J/Ag== MIME-Version: 1.0 In-Reply-To: References: <8b9a5844-66b0-4940-946a-5e626462cdce@googlegroups.com> Date: Thu, 23 Aug 2012 19:29:20 +1000 Subject: Re: Guarding arithmetic From: Chris Angelico 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 18 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345714163 news.xs4all.nl 6972 [2001:888:2000:d::a6]:51927 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27716 On Thu, Aug 23, 2012 at 7:22 PM, Mark Carter wrote: > OK, so it looks like a solution doesn't exist to the problem as specified. I guess it's something that only a language with macros could accommodate. You're asking for a function to prevent the evaluation of its arguments from throwing an error. That's fundamentally not possible with a function call. Exception handling is a much more normal way of handling it; though if you prefer, you could wrap it up in a function like this: def safe_div(num,denom): try: return num/denom except ZeroDivisionError: return 42 print safe_div(1,0) # still a poor name though ChrisA