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


Groups > comp.lang.python > #101042

Re: raise None

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Cameron Simpson <cs@zip.com.au>
Newsgroups comp.lang.python
Subject Re: raise None
Date Thu, 31 Dec 2015 16:45:03 +1100
Lines 101
Message-ID <mailman.101.1451540708.11925.python-list@python.org> (permalink)
References <5684b94d$0$1586$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii; format=flowed
X-Trace news.uni-berlin.de D0cWDRfU3xeldxnWIAGqqwciKNetixJDCOJ0sj1qCq9A==
Return-Path <cameron@cskk.homeip.net>
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; 'none,': 0.05; 'url:bitbucket': 0.05; 'caller': 0.07; 'perl,': 0.07; 'stack.': 0.07; 'cc:addr:python-list': 0.09; '"or': 0.09; 'abort': 0.09; 'learn,': 0.09; 'loop.': 0.09; 'none"': 0.09; 'none.': 0.09; 'subject:None': 0.09; 'bug': 0.10; 'exception': 0.13; 'stack': 0.13; 'def': 0.13; 'do,': 0.15; 'thu,': 0.15; 'value.': 0.15; '39,': 0.16; '>>>i': 0.16; '>on': 0.16; 'argument.': 0.16; 'b):': 0.16; 'foo()': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'funcname': 0.16; 'guess.': 0.16; 'margins': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'no-op,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'simpson': 0.16; 'succinct': 0.16; 'text?': 0.16; 'to:addr:pearwood.info': 0.16; "to:name:steven d'aprano": 0.16; 'unhappy': 0.16; 'url:py': 0.16; 'workings': 0.16; 'wrote:': 0.16; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'meant': 0.22; 'arguments': 0.22; 'exceptions': 0.22; 'gather': 0.22; 'latter': 0.22; 'mind.': 0.22; 'names.': 0.22; 'see:': 0.22; 'cheers,': 0.22; 'pass': 0.22; 'cc:no real name:2**0': 0.22; 'bit': 0.23; 'seems': 0.23; 'wrote': 0.23; 'attached.': 0.23; 'dec': 0.23; 'this:': 0.23; 'second': 0.24; 'import': 0.24; '(most': 0.24; 'plain': 0.24; 'header:In-Reply- To:1': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'point.': 0.27; 'compare': 0.27; 'least': 0.27; 'consult': 0.27; 'function': 0.28; 'idea': 0.28; 'raise': 0.29; "i'm": 0.30; 'certainly': 0.30; "i'd": 0.31; 'error.': 0.31; 'supposed': 0.31; 'another': 0.32; 'especially': 0.32; 'skip:_ 10': 0.32; 'returned': 0.32; 'maybe': 0.33; 'useful': 0.33; 'url:python': 0.33; "d'aprano": 0.33; 'raised': 0.33; 'raising': 0.33; 'steven': 0.33; 'though.': 0.33; 'open': 0.33; 'similar': 0.33; 'file': 0.34; 'could': 0.35; 'requiring': 0.35; 'skip:> 10': 0.35; 'something': 0.35; 'expected': 0.35; 'but': 0.36; 'instead': 0.36; 'there': 0.36; 'url:org': 0.36; 'form,': 0.36; 'keyword': 0.36; 'mode': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'signature': 0.37; 'suggestion': 0.37; 'charset:us-ascii': 0.37; 'things': 0.38; 'no,': 0.38; 'received:localdomain': 0.38; 'stuff': 0.38; 'several': 0.38; 'mean': 0.38; 'end': 0.39; 'sure': 0.39; 'does': 0.39; 'rather': 0.39; 'save': 0.60; 'your': 0.60; 'determine': 0.61; 'skip:u 10': 0.61; 'reach': 0.61; 'today,': 0.62; 'details': 0.62; 'skip:n 10': 0.62; 'course': 0.62; 'is.': 0.63; 'here:': 0.63; 'benefit': 0.66; 'cameron': 0.66; 'detail.': 0.66; 'natural': 0.67; '(is': 0.84; '>def': 0.84; '>you': 0.84; 'horrible': 0.84; 'idiom': 0.84; 'fish': 0.91
Content-Disposition inline
In-Reply-To <5684b94d$0$1586$c3e8da3$5496439d@news.astraweb.com>
User-Agent Mutt/1.5.24 (2015-08-30)
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:101042

Show key headers only | View raw


On 31Dec2015 16:12, Steven D'Aprano <steve@pearwood.info> wrote:
>On Thu, 31 Dec 2015 03:03 pm, Cameron Simpson wrote:
>Steven D'Aprano (that's me) wrote this:
>>>Whereas if _validate does what it is supposed to do, and is working
>>>correctly, you will see:
>>>
>>>Traceback (most recent call last):
>>>  File "spam", line 19, in this
>>>  File "spam", line 29, in that
>>>  File "spam", line 39, in other
>>>  File "spam", line 5, in _validate
>>>ThingyError: ...
>>>
>>>and the reader has to understand the internal workings of _validate
>>>sufficiently to infer that this exception is not a bug in _validate but an
>>>expected failure mode of other when you pass a bad argument.
>>
>> Would it not be useful then to name the including function in the
>> exception text?
>
>You mean change the signature of _validate to:
>
>def _validate(a, b, name_of_caller):
>    ...
>
>and have function "other" call it like this:
>
>def other(arg1, arg2):
>    _validate(arg1, arg2, "other")
>    # if we reach this line, the arguments were validated
>    # and we can continue
>    ...
>
>I think that's pretty horrible. I'm not sure whether that would be more, or
>less, horrible than having _validate automagically determine the caller's
>name by looking in the call stack.

No, I meant your latter suggestion above: consult the call stack to fish out 
the calling function name.  Something like:

  from cs.py.stack import caller
  ...
  def _validate(...):
    frame = caller()
    funcname = frame.functionname

and then use funcname in the messages, or greater detail. Caller() is available 
here:

  https://bitbucket.org/cameron_simpson/css/src/tip/lib/python/cs/py/stack.py?fileviewer=file-view-default

for the fiddliness. The horribleness is at least concealed, unless you're 
nesting _validate implementations.

>> I confess that when I want to check several things I would like to return
>> several failure indications. So thing on that line, how about this:
>>
>>   for blam in _validate(a, b):
>>     raise blam
>>
>> which leaves you open to gatheroing them all up instead of aborting on the
>> first complaint.
>
>Are you sure? I would have expected that raising the first exception would
>exit the loop.

In the bare form, surely, just like your "if". But in principle you could 
gather all the exceptions together and raise a new exception with details 
attached.

>>>I think this is a win for debuggability. (Is that a word?) But it's a bit
>>>annoying to do it today, since you have to save the return result and
>>>explicitly compare it to None. If "raise None" was a no-op, it would feel
>>>more natural to just say raise _validate() and trust that if _validate
>>>falls out the end and returns None, the raise will be a no-op.
>>
>> This is a nice idea though. Succinct and expressive, though people would
>> have to learn that:
>>
>>   raise foo()
>>
>> does not unconditionally abort at this point.
>
>Yes, that crossed my mind. Maybe if there was a second keyword:
>
>    raiseif foo()
>
>which only raised if foo() returned a non-None value. That's kind of like
>the "or die" idiom from Perl, I guess. But of course requiring a second
>keyword will almost certainly doom this proposal -- it is only of benefit
>at the margins as it is.

I'd rather your original myself: plain "raise". Another keyword seems a reach, 
and I don't like it. And I don't like "raiseif"; I've got a bunch of "blahif" 
functions of similar flavour and I'm stuff unhappy with their names.

I think it is a small thing to learn, especially as "raise None" is already an 
error.

Cheers,
Cameron Simpson <cs@zip.com.au>

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