Python equivalent to a C trick

Sam Holden sholden at flexal.cs.usyd.edu.au
Tue Aug 10 03:01:41 EDT 2004


On 9 Aug 2004 23:55:08 -0700, Dan <danmoskowitz at gmail.com> wrote:
> Is there a python equivalent of this trick in C?
>
> Logic_Test ? True_Result : False_Result
>
> Example:
> printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );

print "you have %i %s" % ( num_eggs, ("egg","eggs")[num_eggs>1] )

But I don't know if int(False)==0 and int(True)==1 are actually
guaranteed. And I wouldn't use it since it's ugly (IMHO).

You don't get the "only evaluate one" goodness of ?:

-- 
Sam Holden



More information about the Python-list mailing list