Python equivalent to a C trick
George Yoshida
ml at dynkin.com
Tue Aug 10 03:22:51 EDT 2004
Dan wrote:
> Is there a python equivalent of this trick in C?
>
> Logic_Test ? True_Result : False_Result
Logic_Test and True_Result or False_Result
would be a Python counterpart.
> Example:
> printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );
So the exmaple is converted to:
print "you have %d %s"%(num_eggs, (num_eggs > 1) and "eggs" or "egg")
But be warned that if True_Result is evaluated as False(e.g. True_Result
is an empty string), this doesn't work.
If you don't know what object is considered False in Python, check the
following document.
* 2.3.1 Truth Value Testing
http://docs.python.org/lib/truth.html
--
George
More information about the Python-list
mailing list