[Python-ideas] except expression

Steven D'Aprano steve at pearwood.info
Fri Feb 21 02:01:01 CET 2014


On Thu, Feb 20, 2014 at 05:41:27PM +0000, Phil Connell wrote:

> There's a nice example that just came up in some code I was looking at.
> Paraphrasing:
[...]
> The current spelling to fix this is:
> 
>     elif not getattr(might_be_none, "foo", None):
>         ...

And that won't go away. You can still do that.

> 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.

I disagree that it's ugly. But even if you do, just don't use it in this 
case! Or wrap it in parens. There is no syntax ever created that cannot 
find itself used in an ugly way:

func(("(",")"))


Sometimes you just have to decide that a particular piece of code is 
ugly, and either don't use it, or live with the ugliness. If we're going 
to reject syntax because there are scenarios where it looks a bit ick, 
we're going to reject *everything*.


-- 
Steven


More information about the Python-ideas mailing list