PEP 308 - suggestion for generalising the ternary operator

Erik Max Francis max at alcyone.com
Wed Feb 12 19:26:16 EST 2003


Bengt Richter wrote:

> Grabbing the opportunity for comparison with my latest & greatest,
> where {x} means "treat x as True in a logical expression, but don't
> change the value." Thus the ternary is
>     c and {x} or y
> or optionally for style symmetry
>     c and {x} or {y}

I have another objection to this form that I didn't mention in my
earlier message (beyond the choice of curly braces for a completely
non-dictionary-like purpose).

The problem is that this doesn't change the fundamental issue of C and x
or y -- even if it worked properly in the case where x is false -- is
utterly idiomatic and is totally unintuitive.  It isn't just not pretty,
the first time you see it will take you a few minutes to 1. figure out
what the syntax does in the particular instance you found it in and 2.
what the general intent of the pattern was.

This just introduces a particularly strange form (in my opinion), but
makes the and/or syntax for a conditional expression even _more_
idiomatic, because now you have to remember the curly braces around the
middle item or it doesn't work.

Further, the suggestion of some syntax for this purpose seems really out
of left field:  You're basically talking about something that will cause
an expression to evaluate one way in one context, and another in
another.  (For the purposes of Boolean testing as an argument in and, it
evalutes to True, but when the and operator actually returns it as part
of an expression, it returns x.  That just boggles the mind; in my
opinion, that's like patching a hole by making a bunch of much smaller
ones.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ If you can't fight and you can't flee, flow.
\__/ Robert Elliot
    Physics reference / http://www.alcyone.com/max/reference/physics/
 A physics reference.




More information about the Python-list mailing list