Equivalent to (a ? b : c) ?

Gareth McCaughan Gareth.McCaughan at pobox.com
Wed Dec 22 20:51:11 EST 1999


Rob Hodges wrote:

>> [someone else:]
>>>>>> ((a and (b,)) or (c,))[0]
>>> Someone mentioned that this is idiomatic Python, but actually it is
>>> idiomatic lisp.
..
> Not being an old-time lisper, I guess I must have been speaking crap
> since I have been corrected both on and off-list!  However I recall
> having seen the lazy `and' and `or' exploited together more than a few
> times when wading through the source of various emacs packages and
> quickly becoming familiar with its meaning, making it seem fairly
> natural to me in Python.  

Oh, I see. It's true that short-circuit "and" and "or" do
get used in this kind of way sometimes, but I think this
ought to be used very sparingly; the typical case where it
makes sense is where you have a number of different ways
to try to get a value and you try them until one succeeds.

This makes sense in Lisp more often than it does in Python
because Lisp doesn't have such a multiplicity of false
values (only one, in fact); so it's more often easy to
arrange that success always yields something true.

> Usually it was used in preference to (if a b c) where using the latter
> would have required writing (if a (progn b) c) -- that is, b consists
> of multiple statements.  Of course that is a flow control
> construction; I've never seen it used the way you write above, which
> I think could only qualify as intentional obfuscation.  

I don't think I understand. If B consists of more than one
statement, how do you do the AND/OR thing without a PROGN?

Er, this is getting a bit off-topic...

> Making lists out of b and c, and grabbing the car of result is not
> idiomatic lisp; in fact I completely overlooked that because although
> it doesn't exactly look natural in Python, it's easy enough to see
> what's happening even if you don't know why it was done -- whereas if
> you don't recognise the laziness of `and' and `or', you'll have a
> really hard time seeing what's going on.

Interesting. What makes that construction obscure for me
is mostly the (,,()(((,,),)(,))) stuff, not the "and" and
"or". I don't mean that the parens and commas do more to
stop it being easy to sit down and work out than the
and/or business; I mean that they contribute more to
the fact that the idiom isn't one the eye just glides
happily past, even when you know what it does.

I think I'm saying much more about my own idiosyncrasies
than about what's really idiomatic Python...

>                                           So that was what I saw as
> being the confusing part.  Even though they are lazy in C too (if
> memory serves...),

(Yes, they are. And in Perl.)

>                    you don't often see that exploited (probably
> because you have the conditional operator there...)

Also because in C they do the Wrong Thing. a && b && c isn't
equal to c if a,b,c are all true. This means you *can't*
(ab)use the short-circuit Booleans to get an equivalent of
?:, even if you want to.

> BTW, my derivation of "idiomatic" was intended tongue-in-cheek; not as
> a linguistic analysis.  Sorry to have offended the linguists, though I
> did learn the true derivation, so it was a profitable comment for me
> at least :)  I will offer in exchange that perhaps "idiot" does stem
> from the same root after all, in the sense that it might be
> interpreted as "someone who does things that make sense to themself
> [but to nobody else]".  Food for thought (and more offtopic by the
> minute; sorry).  

"Idiot" comes from the Greek "idiotes" meaning "layman". I'm not
sure why "idiotes" means that; perhaps the idea is the contrast
between the individual layman and the person who's a member of
whatever institution is in question.

-- 
Gareth McCaughan  Gareth.McCaughan at pobox.com
sig under construction



More information about the Python-list mailing list