PEP-308 a "simplicity-first" alternative
holger krekel
pyth at devel.trillke.net
Tue Feb 11 17:44:09 EST 2003
Peter Hansen wrote:
> Paul Rubin wrote:
> [on YATOP]
> > 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
b) that there is an alternative part after it
Everything else would be surprising. From there it's only a short
way to "getting it". 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
i don't think it's "entirely non-obvious" for you anymore?
Sure, it has to be taught to newcomers but only as an "addon"
to boolean expressions and as a rather minor thing. no big deal.
Admitted, it's a bit odd but many of us find seeing 'if' and
'else' (and whatnot) in expressions odd, too. It might not be
100% satisfying but then it is a rather simple change which
fixes one particular problem avoiding to invent
another kind of expression.
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.
regards,
holger
More information about the Python-list
mailing list