the PHP ternary operator equivalent on Python

Peter Otten __peter__ at web.de
Fri Nov 18 14:54:02 EST 2005


Daniel Crespo wrote:

> Oh... Well, thanks for that information.
> 
> I'll do this then:
> 
> def TernaryOperation(condition,true_part,false_part):
>     if condition:
>         return True-part
>     else:
>         return False-part
> 
> a = {'Huge': TernaryOperation(quantity>90,True,False)}

By the time it compiles it will do the same as

a = {"Huge": quantity>90}

Consider describing your actual problem and keep in mind that the "ternary
operator" is a means, not an end.

Peter




More information about the Python-list mailing list