Conditional operator in Python?

thp at cs.ucr.edu thp at cs.ucr.edu
Tue Sep 4 05:29:43 CEST 2001


Marcin 'Qrczak' Kowalczyk <qrczak at knm.org.pl> wrote:
: Sat, 31 Mar 2001 21:22:54 -0800, Erik Max Francis <max at alcyone.com> pisze:

:> The Python FAQ, for instance, suggests x ? a : b can be reliably
:> substituted with
:> 
:>     (x and [a] or [b])[0]

: It computes both a and b. 

No.

: A correct solution is
:     (0 and (lambda: a) or (lambda: b))()

Yes.

: Yes, it is ugly.

Agreed.  Something more aesthetic is definitely needed.  I hate writing:

  factorial = lambda x : (x<=1 and [1] or [x*factorial(x-1)])[0] 

Tom Payne



More information about the Python-list mailing list