[Python-ideas] except expression

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


On 02/19/2014 05:00 PM, Chris Angelico wrote:
> On Thu, Feb 20, 2014 at 11:15 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
>> which would be converted to (using Nick's notation):
>>
>> result = 1/x except ZeroDivisionError -> NaN
>>
>> result = some_func(value1, value2) except SomeError -> 42
>
> These two I strongly support (colon or arrow, either way).
>
>> os.unlink(some_file) except OSError -> None
>
> This one, not so much. You're using os.unlink(some_file) as a
> statement, ignoring its return value. Changing its return value to
> None in the case of an exception isn't exactly what you're trying to
> do. Yes, it does make for a one-liner, where the full form takes two:
>
> try: os.unlink(some_file)
> except OSError: pass
>
> but I'm not sure that it's the right way to do things.

1) It could be used inside a function call or assigned to a variable;

2) We're creating a new expression type -- it'll be valid anywhere an expression is valid, even all by itself.  e.g. `1 
+ 2` is valid, even though nothing is done with the result.

--
~Ethan~


More information about the Python-ideas mailing list