Conditional operator in Python?

Weet Vanniks Weet.Vanniks at el_simpatico.be
Tue Sep 4 03:09:38 EDT 2001


What about :

def cond_choose(cond,first_choice,second_choice):
     if cond:
          return first_choice
     return second_choice

whatever= cond_choose(x,a,b)

This seems much cleaner to me than all those contorsions with the syntax.

Weet

thp at cs.ucr.edu wrote:

> 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