Path: csiph.com!feeder.erje.net!2.eu.feeder.erje.net!newsfeed.freenet.ag!newsfeed0.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Mark Lawrence Newsgroups: comp.lang.python Subject: Re: raise None Date: Thu, 31 Dec 2015 15:58:56 +0000 Lines: 79 Message-ID: References: <56847239$0$1590$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de wsldt5k3d3snUSf+WskBAAoY1QBQkU1MuYv2ujbfwSKQ== 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; 'from:addr:yahoo.co.uk': 0.05; 'none:': 0.05; 'rename': 0.07; 'naturally': 0.09; 'none"': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:None': 0.09; 'valueerror': 0.09; 'exception': 0.13; 'def': 0.13; 'argument': 0.15; 'b):': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'think?': 0.16; 'traceback.': 0.16; 'wrote:': 0.16; 'obviously': 0.16; 'language': 0.19; 'work,': 0.21; 'function:': 0.22; 'lawrence': 0.22; 'suppose': 0.22; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; 'error': 0.27; 'checking': 0.27; 'time:': 0.27; 'raise': 0.29; 'code': 0.30; 'language.': 0.32; "d'aprano": 0.33; 'steven': 0.33; 'though.': 0.33; 'traceback': 0.33; 'could': 0.35; 'nothing.': 0.35; 'level': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'where': 0.40; 'mark': 0.40; 'save': 0.60; 'spam.': 0.61; 'charset:windows-1252': 0.62; 'more': 0.63; 'our': 0.64; 'repeat': 0.67; 'gain': 0.82; 'confusing': 0.84; 'irrelevant': 0.84; 'pythonistas,': 0.84; 'unnecessary,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: ip13.worldwideprinting.adsl.gxn.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.0 In-Reply-To: <56847239$0$1590$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101060 On 31/12/2015 00:09, Steven D'Aprano wrote: > I have a lot of functions that perform the same argument checking each time: > > def spam(a, b): > if condition(a) or condition(b): raise TypeError > if other_condition(a) or something_else(b): raise ValueError > if whatever(a): raise SomethingError > ... > > def eggs(a, b): > if condition(a) or condition(b): raise TypeError > if other_condition(a) or something_else(b): raise ValueError > if whatever(a): raise SomethingError > ... > > Since the code is repeated, I naturally pull it out into a function: > > def _validate(a, b): > if condition(a) or condition(b): raise TypeError > if other_condition(a) or something_else(b): raise ValueError > if whatever(a): raise SomethingError > > def spam(a, b): > _validate(a, b) > ... > > def eggs(a, b): > _validate(a, b) > ... > > But when the argument checking fails, the traceback shows the error > occurring in _validate, not eggs or spam. (Naturally, since that is where > the exception is raised.) That makes the traceback more confusing than it > need be. I disagree. > > So I can change the raise to return in the _validate function: > > def _validate(a, b): > if condition(a) or condition(b): return TypeError > if other_condition(a) or something_else(b): return ValueError > if whatever(a): return SomethingError > > and then write spam and eggs like this: > > def spam(a, b): > ex = _validate(a, b) > if ex is not None: raise ex > ... > > It's not much of a gain though. I save an irrelevant level in the traceback, > but only at the cost of an extra line of code everywhere I call the > argument checking function. > > But suppose we allowed "raise None" to do nothing. Then I could rename > _validate to _if_error and write this: > > def spam(a, b): > raise _if_error(a, b) > ... > > > and have the benefits of "Don't Repeat Yourself" without the unnecessary, > and misleading, extra level in the traceback. > > Obviously this doesn't work now, since raise None is an error, but if it did > work, what do you think? > A lot of fuss over nothing. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence