[Python-ideas] "or raise" syntax
Antony Lee
antony.lee at berkeley.edu
Mon Nov 3 22:38:43 CET 2014
If anything, a better idea would be to make "raise [... [from ...]]" an
expression (possibly requiring parentheses, à la yield), thus allowing it
to be put in constructs such as the one suggested, but also in lambda
expressions.
Of course, the value of the expression doesn't have to be specified (the
assignment target stays unbound, causing a NameError if we somehow catch
the exception and later refer to the assignment target).
2014-10-30 11:45 GMT-07:00 Terry Reedy <tjreedy at udel.edu>:
> On 10/30/2014 11:13 AM, Ethan Furman wrote:
>
>> On 10/30/2014 08:03 AM, Javier Dehesa wrote:
>>
>>>
>>> This happens to me with some frequency:
>>>
>>> result = f(args)
>>> if not result: # of "if result is None:"
>>> raise Exception(...)
>>>
>>> What if I could just say?
>>>
>>> result = f(args) or raise Exception(...)
>>>
>>
>> Seems like a decent idea, but you can already have most of that:
>>
>> result = f(args) or raise_exc(ValueError, 'args must be ...')
>>
>> and then have 'raise_exc' do the exception raising work.
>>
>
> No need to pass exception class and message separately instead of an
> exception instance.
>
> def raiser(exc):
> raise exc
>
> print(1 or raiser(ValueError('null value')))
> print(0 or raiser(ValueError('null value')))
>
> >>>
> 1
> Traceback (most recent call last):
> File "c:\programs\python34\tem.py", line 5, in <module>
> print(0 or raiser(ValueError('null value')))
> File "c:\programs\python34\tem.py", line 2, in raiser
> raise exc
> ValueError: null value
>
> It does add another line to the trackback, but this is pretty minor.
>
> --
> Terry Jan Reedy
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141103/fa1df1ce/attachment-0001.html>
More information about the Python-ideas
mailing list