[Python-ideas] except expression
Ben Finney
ben+python at benfinney.id.au
Thu Feb 13 01:28:42 CET 2014
Ben Finney <ben+python at benfinney.id.au> writes:
> Ram Rachum <ram.rachum at gmail.com> writes:
>
> > Here's an idea that would help shortening code. Allow a ternary
> > expression based on except, like so:
> >
> > first_entry = entries[0] except IndexError else None
> > item = my_queue.get() except queue.Empty else None
> > response_text = request('http://whatever.com').text except HttpError else "Can't access data"
>
> That is more obscure, to my eye, than laying out the control branches:
Sorry, I failed to address your first two examples.
I am +0 on the proposal to have something similar to Perl's fallback
syntax, “$foo = bar() or some_default_value”.
Yet I still find the proposed syntax less readable for anything but a
trivial *and* brief case. For anything longer than a few dozen
characters, I still prefer::
try:
response_text = request('http://whatever.com').text
except HttpError:
"Can't access data"
for being explicit and clarifying what to expect.
--
\ “I wish there was a knob on the TV to turn up the intelligence. |
`\ There's a knob called ‘brightness’ but it doesn't work.” |
_o__) —Eugene P. Gallagher |
Ben Finney
More information about the Python-ideas
mailing list