Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101037
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Terry Reedy <tjreedy@udel.edu> |
| Newsgroups | comp.lang.python |
| Subject | Re: raise None |
| Date | Wed, 30 Dec 2015 23:00:16 -0500 |
| Lines | 97 |
| Message-ID | <mailman.99.1451534427.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; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de mb9YcV8NrqAZnNJGReVlFAfR9xyhyMrnbsAV9Xqt/kxQ== |
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'elif': 0.04; 'none:': 0.05; 'rename': 0.07; 'compute': 0.09; 'err:': 0.09; '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; 'jan': 0.11; 'exception': 0.13; 'def': 0.13; 'argument': 0.15; 'b):': 0.16; "chris'": 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'think?': 0.16; 'traceback.': 0.16; 'wrote:': 0.16; 'obviously': 0.16; 'try:': 0.18; 'work,': 0.21; 'function:': 0.22; 'suppose': 0.22; 'bit': 0.23; '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; 'not.': 0.27; 'time:': 0.27; 'ex:': 0.29; 'reduced': 0.29; 'raise': 0.29; 'code': 0.30; 'maybe': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'though.': 0.33; 'traceback': 0.33; 'similar': 0.33; 'except': 0.34; 'could': 0.35; 'nothing.': 0.35; 'level': 0.35; 'but': 0.36; 'too': 0.36; 'received:71': 0.36; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'one,': 0.37; 'skip:z 10': 0.38; 'copying': 0.38; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'save': 0.60; 'spam.': 0.61; 'more': 0.63; 'repeat': 0.67; 'gain': 0.82; 'confusing': 0.84; 'idiom': 0.84; 'irrelevant': 0.84; 'unnecessary,': 0.84; 'received:fios.verizon.net': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | pool-71-185-227-36.phlapa.fios.verizon.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 <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:101037 |
Show key headers only | View raw
On 12/30/2015 7:09 PM, 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.
>
> 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.
It is nicer than the similar standard idiom
try:
_validate(a, b)
except Exception as e:
raise e from None
If you could compute a reduced traceback, by copying one, then this
might work (based on Chris' idea.
def _validate(a, b):
ex = None
if condition(a) or condition(b): ex = TypeError
elif other_condition(a) or something_else(b): ex = ValueError
elif whatever(a): ex = SomethingError
if ex:
try: 1/0
except ZeroDivisionError as err: tb = err.__traceback__
tb = <copy of tb with this frame omitted>
raise ex.with_traceback(tb)
> 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?
Perhaps a bit too magical, but maybe not.
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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