Conditional operator in Python?

Duncan Booth duncan at rcp.co.uk
Mon Apr 2 06:27:37 EDT 2001


Sheila King <sheila at spamcop.net> wrote in 
<of4dct0nul0rc6ik758gb1io3i099q1bgo at 4ax.com>:

>So, to return the max of a or b I could do
>
>return  ( a > b ) and a or b
>
>Here is an interpreter session:
>
>>>> a = 5
>>>> b = 3
>>>> (a > b) and a or b
>5
>>>> a = 2
>>>> b = 12
>>>> (a > b) and a or b
>12
>>>> 
>

Please don't do this, as it doesn't do what you think.

>>> a = 0
>>> b = -5
>>> (a > b) and a or b
-5

and consider that one of the common uses for a max function is to set a 
lower bound of 0 on a calculation.



More information about the Python-list mailing list