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


Groups > comp.lang.python > #101032

Re: raise None

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: raise None
Date Thu, 31 Dec 2015 11:38:12 +1100
Lines 44
Message-ID <mailman.96.1451522295.11925.python-list@python.org> (permalink)
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 <rosuav@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:101032

Show key headers only | View raw


On Thu, Dec 31, 2015 at 11:09 AM, Steven D'Aprano <steve@pearwood.info> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

raise None Steven D'Aprano <steve@pearwood.info> - 2015-12-31 11:09 +1100
  Re: raise None Paul Rubin <no.email@nospam.invalid> - 2015-12-30 16:19 -0800
  Validation in Python (was: raise None) Ben Finney <ben+python@benfinney.id.au> - 2015-12-31 11:26 +1100
  Re: raise None Chris Angelico <rosuav@gmail.com> - 2015-12-31 11:38 +1100
    Re: raise None Steven D'Aprano <steve@pearwood.info> - 2015-12-31 12:26 +1100
      Re: raise None Ben Finney <ben+python@benfinney.id.au> - 2015-12-31 12:44 +1100
        Re: raise None Steven D'Aprano <steve@pearwood.info> - 2015-12-31 15:07 +1100
          Re: raise None Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-12-31 12:19 +0000
            Re: raise None Steven D'Aprano <steve@pearwood.info> - 2016-01-01 02:35 +1100
              Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-01 02:53 +1100
              Re: raise None Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-12-31 16:46 +0000
                Re: raise None Steven D'Aprano <steve@pearwood.info> - 2016-01-01 04:50 +1100
              Re: raise None "Martin A. Brown" <martin@linux-ip.net> - 2015-12-31 09:30 -0800
              Re: raise None Ben Finney <ben+python@benfinney.id.au> - 2016-01-01 07:18 +1100
                Re: raise None Johannes Bauer <dfnsonfsduifb@gmx.de> - 2016-01-02 12:47 +0100
              Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-01 09:48 +1100
                Re: raise None Steven D'Aprano <steve@pearwood.info> - 2016-01-04 16:19 +1100
                Re: raise None Dan Sommers <dan@tombstonezero.net> - 2016-01-04 06:09 +0000
                Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-03 22:39 -0800
              Re: raise None Ben Finney <ben+python@benfinney.id.au> - 2016-01-01 10:27 +1100
                Re: raise None Marko Rauhamaa <marko@pacujo.net> - 2016-01-01 02:29 +0200
                Re: raise None Steven D'Aprano <steve@pearwood.info> - 2016-01-04 16:19 +1100
                Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-03 21:53 -0800
                Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-04 03:55 -0800
                Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-03 21:53 -0800
              Re: raise None Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-31 23:36 +0000
              Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-01 10:39 +1100
              Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-01 10:41 +1100
              Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-03 19:04 -0800
                Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-04 14:31 +1100
                Re: raise None Steven D'Aprano <steve@pearwood.info> - 2016-01-04 14:48 +1100
                Re: raise None Chris Angelico <rosuav@gmail.com> - 2016-01-04 14:56 +1100
                Re: raise None Rustom Mody <rustompmody@gmail.com> - 2016-01-03 20:46 -0800
              Re: raise None Christian Gollwitzer <auriocus@gmx.de> - 2016-01-04 08:28 +0100
      Re: raise None Chris Angelico <rosuav@gmail.com> - 2015-12-31 13:12 +1100
      Re: raise None Cameron Simpson <cs@zip.com.au> - 2015-12-31 15:03 +1100
        Re: raise None Steven D'Aprano <steve@pearwood.info> - 2015-12-31 16:12 +1100
          Re: raise None Cameron Simpson <cs@zip.com.au> - 2015-12-31 16:45 +1100
  Re: raise None Terry Reedy <tjreedy@udel.edu> - 2015-12-30 23:00 -0500
  Re: raise None Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-31 15:58 +0000
  Re: raise None Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-12-31 17:17 +0100

csiph-web