Conditional operator in Python?

thp at cs.ucr.edu thp at cs.ucr.edu
Tue Sep 4 12:31:56 EDT 2001


Michael Abbott <michael at rcp.co.uk> wrote:
: Marcin 'Qrczak' Kowalczyk <qrczak at knm.org.pl> wrote in
: news:slrn9p942t.fg.qrczak at qrnik.zagroda: 

:> Tue, 4 Sep 2001 07:24:13 +0000 (UTC), thp at cs.ucr.edu <thp at cs.ucr.edu>
:> pisze: 
:> 
:> IMHO this would look more clearly:
:> 
:>     p = lambda k, n:
:>       if k < 1  or k > n  then 0 else
:>       if n == 1 or n == k then 1 else
:>       p( k-1, n-1 ) + p (k, n-k )
:> 

: I think (though it would need a bit of careful checking) that there is no 
: conflict with the existing Python syntax in making

:     	if <expr> then <expr> else <expr>

: an expression.

: Perhaps adding this to the syntax could be a PEP?  Obviously the semantics 
: of the expression

:     	if a then b else c

: could be explained as equal to the expression

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

Excellent idea!  To be really useful, Lambda abstraction must be used
in conjunction with short-circuiting selection.

Lambda abstraction isn't for everyone or every situation.  It can,
however, be extremely elegant for some situations, especially when
combined with other features of Python.  Consider, for instance, the
following general layout for defining mutually recursive functions:

   f, g = 
     lambda x : ... f ... g ... x ... ,  # an expression in f, g, and x
     lambda x : --- f --- g --- x ---    # another such expression

Tom Payne




More information about the Python-list mailing list