Trinary operator?

Christophe Delord christophe.delord at free.fr
Wed Apr 17 15:20:44 EDT 2002


You have to be very carefull with and/or. If you evaluate A and B or C, if A is true and B is false, the result is C !!!

For example, "number=='zero' and 0 or 1" returns 1 whatever the value of number

>>> number='one'
>>> number=='zero' and 0 or 1
1
>>> number='zero'
>>> number=='zero' and 0 or 1
1
>>> 

Christophe.

On Wed, 17 Apr 2002 13:02:56 -0500
"Mark McEahern" <marklists at mceahern.com> wrote:

> > Not that it's a big deal since there's many ways to do that, but can I use
> > something like
> >     verboseGender = (gender == 'm') ? 'male' : 'female'
> > in Python?
> 
> People seem to use and/or for that:
> 
> >>> gender = 'm'
> >>> verbose = (gender == 'm' and 'male' or 'female')
> >>> verbose
> 'male'
> >>> gender = 'f'
> >>> verbose = (gender == 'm' and 'male' or 'female')
> >>> verbose
> 'female'
> 
> // mark
> 
> 


-- 
Christophe Delord
http://christophe.delord.free.fr/



More information about the Python-list mailing list