[Python-Dev] PEP 463: Exception-catching expressions

Chris Angelico rosuav at gmail.com
Mon Feb 24 01:50:09 CET 2014


On Mon, Feb 24, 2014 at 7:51 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
>> Yes. Augmented assignment is still assignment, so a statement. The only
>> way to parse that is as
>>
>> x /= (y except ZeroDivisionError: 1)
>
>
> Well, that is certainly not what I would have expected.

I can see that you'd want to have that go back and redo the division
with a second argument of 1, which'd look like this in statement form:

try: x /= y
except ZeroDivisionError: x /= 1

But, just like the decried error suppression technique, the second
half of this is something that should instead be written "pass". At
very least, I'd say that an except-expression where one or other of
its forms is better spelled "pass" is code smell, and at worst, I'd
say it's a hint that the expression form might not even be what you
think it is - as in this case.

Remember, this is a scope-narrowing. Where previously you had to
try/except entire statements, now you can try/except just one part of
something. That means you won't catch errors in the actual assignment
- which is usually a good thing - but it does affect augmented
assignment. My recommendation: Just use try... except pass. I'm not
trying to supplant the statement form :)

ChrisA


More information about the Python-Dev mailing list