Equivalent to (a ? b : c) ?

Rob Hodges s323140 at student.uq.edu.au
Tue Dec 21 11:14:52 EST 1999


Justin Sheehy <dworkin at ccs.neu.edu> writes:

> "Jesper Hertel" <jh at cddk.dk> writes:
> 
> > > ((a and (b,)) or (c,))[0]
> 
> > I hope this was a joke. That kind of constructions is impossible to read for
> > other programmers, making the program hard to maintain.

(Perl anyone?)  But really, I think it's a bit much to ask for a
readable version of ?: since the original construct is itself a bit of
a disaster for readability IMO...

Someone mentioned that this is idiomatic Python, but actually it is
idiomatic lisp.  So if the other programmers you mention happen to be
lisp programmers, they'll recognise it right off the bat!  And in
truth, if they are Python programmers with any significant experience,
they will too... which I guess is what idiomatic means -- an idiocy so
common that recognising it quickly becomes automatic.  (Thereby making
it seem less of an idiocy.)  

> > I like maxm's function much better, because you can easily understand what
> > the meaning is.

Or at least, you can understand the intention... ;)

Out of curiosity, is there any advantage to using singleton tuples
rather than lists?  And is there any advantage in using the
seemingly-redundant parens?

Is not

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

exactly equivalent to the above snippet?  Or is there a speed
disadvantage or obscure special case to shoot me down?  I just dislike
singleton tuples; they look awkward... and the mix of bracket styles
would, in spite of what I already said, help readability when function
calls were involved -- ie, out of

((parrot(x) and (parrot(y),)) or (parrot(z),))[0]

(parrot(x) and (parrot(y),) or (parrot(z),))[0]

(parrot(x) and [parrot(y)] or [parrot(z)])[0]

mentally matching the brackets gets easier as you go down.  Provided
you actually know the precedence rules, you can mentally treat the
`and' as binding its arguments together -- which is easier than
scanning across looking for a paren that matches.

-Rob




More information about the Python-list mailing list