[Python-ideas] Make "raise" an expression
Michael Selik
michael.selik at gmail.com
Sun Aug 7 00:20:42 EDT 2016
On Sat, Aug 6, 2016 at 10:13 PM Terry Reedy <tjreedy at udel.edu> wrote:
> On 8/6/2016 8:57 PM, Michael Selik wrote:
> > On Sat, Aug 6, 2016, 7:24 PM אלעזר
> > <elazarg at gmail.com
> > <mailto:elazarg at gmail.com>> wrote:
> > The raise statement cannot be used as an expression, even though
> > there is no theoretical reason for it not to. I don't see any
> > practical reason either - except the fact that it requires changing
> > the syntax.
> >
> > I have found this idea mentioned by Anthony Lee in a thread from
> > 2014, regarding "or
> > raise",
> https://mail.python.org/pipermail/python-ideas/2014-November/029921.html.
> But there
> > is nothing against the idea there.
> >
> > Yes, it could easily be implemented using a function, but this seems
> > to be an argument against *augmenting* the language, not against
> > *changing* it. Moreover, this function will have uglier name, since
> > `raise` is taken. (It could have been the other way around -
> > omitting the statement entirely, leaving only a built in function
> > `raise()`. I'm not suggesting that, of course)
> >
> >
> > Luckily, "throw" is available for you.
> >
> > def throw(exception):
> > raise exception
>
> Nice. This makes the 'or raise' construct available to any who like it
> in all versions. There is precedent for this name in generator.throw.
>
I suppose it should really be more like
In [1]: def throw(exception=None, *, cause=None):
...: if exception is None:
...: raise
...: if cause is None:
...: raise exception
...: raise exception from cause
I'm not sure if I like the use in a shortcut expression. Too easy to
accidentally use ``or`` when you mean ``and`` and vice versa.
False or throw(RuntimeError('you shoulda been True to me'))
True and throw(RuntimeError('I knew it was you, Fredo.'))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160807/23f0e7a7/attachment-0001.html>
More information about the Python-ideas
mailing list