Conditional operator in Python?

Mikael Olofsson mikael at isy.liu.se
Tue Sep 4 03:48:13 EDT 2001


On 04-Sep-2001 thp at cs.ucr.edu wrote:
 >  Okay, factorial is a somewhat simplistic example.  Consider the
 >  following function, which counts the k-way partitions of n:
 >  
 >    p = lambda k, n : (
 >        ( k < 1  or k > n  ) and [0] or 
 >        ( n == 1 or n == k ) and [1] or 
 >        [p(k-1,n-1)+p(k,n-k)] 
 >      )[0]
 >  
 >  Using, say, ?: notation this would be written:
 >  
 >    p = lambda k, n : 
 >      k < 1  or k > n  ? 0 :
 >      n == 1 or n == k ? 1 :
 >      p( k-1, n-1 ) + p (k, n-k )
 >  
 >  which seems much more concise and readable.

Well, both are just as hard to read to me, but then I have never used 
C. I (almost) always use if-then-else, since that fits my small brain 
best. Sure, it needs more rows, but I prefer that to both variations of
line noise above.

/Mikael

-----------------------------------------------------------------------
E-Mail:  Mikael Olofsson <mikael at isy.liu.se>
WWW:     http://www.dtr.isy.liu.se/dtr/staff/mikael               
Phone:   +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
Date:    04-Sep-2001
Time:    09:38:44

         /"\
         \ /     ASCII Ribbon Campaign
          X      Against HTML Mail
         / \

This message was sent by XF-Mail.
-----------------------------------------------------------------------
Linköpings kammarkör: www.kammarkoren.com




More information about the Python-list mailing list