[Python-ideas] except expression

Ethan Furman ethan at stoneleaf.us
Thu Feb 20 01:43:20 CET 2014


On 02/19/2014 04:22 PM, Yury Selivanov wrote:
>
> If we add new 'raises' keyword, then we can have the following
> new syntax:
>
> # set 'value' to 42 if len(a) < 10
> # or else, return a[10]
> value = 42 if a[10] raises IndexError
>
> # set 'value' to 42 if len(a) < 10
> # or else, return 'spam'
> value = 42 if a[10] raises IndexError else 'spam'
>
> # same as above but if a[10] raises anything
> value = 42 if a[10] raises
>
> # another example
> foo(None if dct['key'] raises)

The big problems I see with this idea are that:

   - the emphasis is placed on the unusual rather than the normal result
   - it's not clear what the normal result should be

For the second point, consider:

  value = 42 if a[10] raises

There is nothing there to indicate that the value of a[10] is what should be used if no exception is raised.

-1

--
~Ethan~


More information about the Python-ideas mailing list