Conditional operator in Python?

thp at thp at
Tue Sep 4 05:29:43 CEST 2001

Marcin 'Qrczak' Kowalczyk <qrczak at> wrote:
: Sat, 31 Mar 2001 21:22:54 -0800, Erik Max Francis <max at> 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. 


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


: 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