Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: raise None Date: Thu, 31 Dec 2015 11:38:12 +1100 Lines: 44 Message-ID: References: <56847239$0$1590$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de xJm48rxu4axvI/U3ZvQDcw2klCBtBx3dcBVtP/wrSRKA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'cc:addr:python-list': 0.09; 'naturally': 0.09; 'subject:None': 0.09; 'valueerror': 0.09; 'exception': 0.13; 'def': 0.13; 'argument': 0.15; 'thu,': 0.15; '11:09': 0.16; 'b):': 0.16; 'bug).': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:209.85.213.176': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '31,': 0.22; 'function:': 0.22; 'am,': 0.23; 'dec': 0.23; 'header :In-Reply-To:1': 0.24; 'error': 0.27; 'checking': 0.27; 'message- id:@mail.gmail.com': 0.27; 'time:': 0.27; 'function': 0.28; 'raise': 0.29; 'code': 0.30; 'problem': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'traceback': 0.33; 'received:google.com': 0.35; "isn't": 0.35; 'but': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'where': 0.40; 'your': 0.60; 'spam.': 0.61; 'more': 0.63; 'sounds': 0.76; 'chrisa': 0.84; 'confusing': 0.84; 'to:none': 0.91 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:cc :content-type; bh=F5M4wVqmN4OXGEosd8GeJHDj1h1adTaSM2b0eHea20U=; b=xnzp6+2mhE3JMyFpvjWKhDpJPaiurfCnxmz7qxDu3SGYFt1yvJVwwr2A1qjraN0dNl FqZK+KU8CYQY974T7V00LtaFnF7CoDpnNZWubeD8HkYyZGTd98xs4I/IYfpsqQL01yj0 4M+ySDw0BUUDHuUhzsq/t9ej9bs7j1rYLMKPbIXnIWG0B07sWyjzZ1jnpKSogNawVCDu iDd23DnrXufglmXqGnu/tC5JIfMpILEZgOI1qc2Erm3jHhODSQ2FRAHbJdBBlMZ67kQa j+NcIwbue/ROteBWptENzcaFhMQuIkHD15qlMiW8tCe0mpNAUhTm+X07gkp/77w7kKvm 3LXQ== X-Received: by 10.50.70.38 with SMTP id j6mr65075575igu.13.1451522292394; Wed, 30 Dec 2015 16:38:12 -0800 (PST) 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:101032 On Thu, Dec 31, 2015 at 11:09 AM, 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. If the validation really is the same in all of them, then is it a problem to see the validation function in the traceback? Its purpose isn't simply "raise an exception", but "validate a specific set of inputs". That sounds like a perfectly reasonable traceback line to me (imagine if your validation function has a bug). ChrisA