[Python-ideas] except expression

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Feb 20 05:17:23 CET 2014


Ethan Furman wrote:

> result = 1/x except ZeroDivisionError -> NaN
> 
> os.unlink(some_file) except OSError -> None
> 
> result = some_func(value1, value2) except SomeError -> 42

I would have a hard time getting used to this
usage of '->'. Nothing else in Python uses it
in an expresson like this. It just seems very
arbitrary.

Here's another idea:

    result = 1/x except {ZeroDivisionError: NaN}

You can read the part in {...} as a dictionary
(although it needn't be implemented that way)
so it doesn't use colons in any way they aren't
being used already.

-- 
Greg


More information about the Python-ideas mailing list