Python equivalent to a C trick

Mark Bottjer mark_bottjer at hotmail.com
Wed Aug 11 14:22:46 EDT 2004


Dan wrote:
> Is there a python equivalent of this trick in C?
> 
> Logic_Test ? True_Result : False_Result

There's something close:

Logic_Test and True_Result or False_Result

print 'you have %i egg%s' % (num_eggs, (num_eggs != 1) and 's' or '')

The main problem with this is that False_Result will get executed if 
Logic_Test is false *or* True_Result evaluates to false. Witness the 
following broken variation of the above:

print 'you have %i egg%s' % (num_eggs, (num_eggs == 1) and '' or 's')

So it's not perfect, but it is workable. And as a previous post 
indicated, this situation is not likely to change. :)

   -- Mark



More information about the Python-list mailing list