[Python-ideas] except expression
Phil Connell
pconnell at gmail.com
Thu Feb 20 18:41:27 CET 2014
On Thu, Feb 20, 2014 at 12:18:07PM +1100, Chris Angelico wrote:
> On Thu, Feb 20, 2014 at 11:15 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> > result = 1/x except ZeroDivisionError -> NaN
> >
> > For the record, I could just as easily live with the colon instead of the
> > arrow.
> >
>
> Time to open up this branch of the discussion... colon or arrow?
>
> For the purposes of this debate, I'm comparing these two notations,
> and nothing else:
>
> result = 1/x except ZeroDivisionError -> NaN
> result = 1/x except ZeroDivisionError: NaN
There's a nice example that just came up in some code I was looking at.
Paraphrasing:
if some_cond:
...
elif not might_be_none.foo:
...
else:
...
There's a bug in the elif expression, namely 'might_be_none' might be ..None :)
The current spelling to fix this is:
elif not getattr(might_be_none, "foo", None):
...
I'm was a fan of the colon version, but it's *really* ugly in this case:
elif not might_be_none.foo except AttributeError: None:
...
': None:' is just awful.
In this case, -> looks much better IMO:
elif not might_be_none.foo except AttributeError -> None:
...
Cheers,
Phil
More information about the Python-ideas
mailing list