Python equivalent to a C trick

Asun Friere afriere at yahoo.co.uk
Wed Aug 11 02:47:16 EDT 2004


Erik Max Francis <max at alcyone.com> wrote in message news:<41189FD5.7516D933 at alcyone.com>...
> Thomas Krüger wrote:
> 
> > Logic_Test and True_Result or False_Result
> > 
> > Example:
> > print (num_eggs > 1) and 'eggs" or "egg"
> 
> Be very careful that the "True_Result" in your formulation actually is
> itself a Python true value, or this won't work:
> 
> 	print 'egg' + (numEggs == 1 and '' or 's')
> 
> will not work as expected.

For which reason the form (test and [true_result] or
[false_result])[0] is sometimes advocated.  This starts to look a
little too cluttered for my liking, so I tend to use it only when the
'true_result' is a variable and might be instantiated to a negative
value.



More information about the Python-list mailing list