Equivalent to (a ? b : c) ?

Rob Hodges s323140 at student.uq.edu.au
Wed Dec 22 04:58:58 EST 1999


Gareth McCaughan <Gareth.McCaughan at pobox.com> writes:

> Rob Hodges wrote:
> 
> [someone else:]
> >>>> ((a and (b,)) or (c,))[0]
> > Someone mentioned that this is idiomatic Python, but actually it is
> > idiomatic lisp.
> 
> Eh?
> 
> Certainly (if a b c) is idiomatic Lisp with the same meaning,
> but the particular hack involved certainly isn't. You *could*
> say something like
>   (car (or (and a (list b)) (list c)))
> but that would be horribly unidiomatic Lisp. I'm not sure
> what the point is here.

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.  

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.  

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.  So that was what I saw as
being the confusing part.  Even though they are lazy in C too (if
memory serves...), you don't often see that exploited (probably
because you have the conditional operator there...)


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).  

Cheers,
-Rob




More information about the Python-list mailing list