PEP-308 a "simplicity-first" alternative

Peter Hansen peter at engcorp.com
Wed Feb 12 08:33:11 EST 2003


holger krekel wrote:
> 
> Peter Hansen wrote:
> > Paul Rubin wrote:
> > > In fact it's not even obvious what it does.
> > > What should the value of (2 and 0 else 3) be?
> >
> > Hmm... (2 and 0) gives the value 0, so the result should clearly
> > be whatever (0 else 3) returns, which logically should be 3, right?
> >
> > (In other words, I agree.  If you know it's a ternary op, you can
> > figure it out.  If you don't, it's entirely non-obvious what it's
> > doing, and reads to me more like a latent syntax error than anything
> > else.)
> 
> Every ternary op construct needs time to get used to it, i am afraid.
> To me 'else' doesn't hint at a boolean operator like 'or' but
> 
>     a) that there is a "not-else" part before it

When I get to the "else" in that statement, my brain just goes "huh?".
It stops cold, because "else" and "and" have no semantic relationship
whatsoever to each other.  I'm sorry, but my brain just doesn't parse
it even though I know at one level what it's *supposed* to mean.

> Everything else would be surprising. 

You're not supposed to have to run through the process of elimination
before concluding what's surprising or not.

> In real life examples (taken from stdlib) like
> 
>     run==1 and '' else 's'
>     cl and cl.__name__ else ''
>     unicode and self.ugettext else self.gettext

These do nothing for me.  No feeling of comprehensions at all.
They just sit flat on the screen, devoid of meaning and waiting
for some kind of translation to words that speak to me.

> i don't think it's "entirely non-obvious" for you anymore?

I have to say our brains work in vastly different ways.  Not only
is it still entirely non-obvious, but no matter how much I look
at it (up to the finite amount tried to date, anyway), the 
"and" part gives no sense of a conditional whatsoever.  In fact,
that's a reason to dislike the current idiom (a and b or c) even 
apart from the fact that it encourages certain bugs.

> Also it's nice that if you discover you don't need an else-part
> anymore you can just delete it.  With the other proposals you have
> to rewrite your expression - using another idiom.

Any time someone writes "it's nice" or "it's a neat trick" in
a discussion of this nature, I'd like to run screaming from
the room...  we _eschew_ neat tricks, not embrace them!

-Peter




More information about the Python-list mailing list