Re: Some python comments/questions [ #1: "?:" equivalent ]

Tim Peters (tim@ksr.com)
Tue, 12 Oct 93 11:42:39 -0400

Always happy to raise the level of illness <wink>: combining the sick
ideas presented so far, I believe a faithful translation of "a?b:c" is:

(a and [b] or [c])[0]

as in:

>>> (1<2 and 0 or 4) # oops
4
>>> (1<2 and [0] or [4])[0] # bliss
0
>>>

That is, we hide b and c inside singleton lists; then [b] and [c] are
always "true", regardless of the values of b and c; so short-circuiting
reliably returns the desired one of "[b]" and "[c]"; and strip the value
out of the list at the end. This also worms around the multiple-evaluation
problem.

Of course I'd rather have my eyes eaten out by a horde of tiny wasps than
actually _use_ this idiom (or-- worse --be forced to read someone else's
code that used it), but it just goes to show once again that Guido
anticipated all possible complaints years before we thought of them <wink>.

Seriously, Python isn't an "expression" language (I'd call it a
"statement" language, hoping the intuitive distinction is clear), so
things "like this" don't seem (to me) to fit well in it: I stopped
missing "?:" early on (perhaps because min and max are built in!), and by
now am 95% reconciled to assignment being a statement instead of an
expression. If "?:" absolutely had to be added to Python, I expect that
"if a then b else c" would be an OK way to spell it.

on-the-other-hand-a-"case"-statement-is-a-natural!-ly y'rs - tim

Tim Peters tim@ksr.com
not speaking for Kendall Square Research Corp