stumbled on python iif() equivalent, at last

Laurent Szyster laurent.szyster at q-survey.be
Sat Jun 23 19:08:43 EDT 2001


Robin Becker wrote:
> 
> In article <3B34B8DE.67D97C24 at q-survey.be>, Laurent Szyster
> <laurent.szyster at q-survey.be> writes
> >so, there's an iif (condition, is_true, is_false) python equivalent:
> >
> >    >>> 1 and 'c' or 'o'
> >    'c'
> >    >>> 0 and 'c' or 'o'

and of course,

> >    'o'


> >Laurent
> yes, but what if the true value is actually a python false eg
> >>> a=''
> >>> b='B'
> >>> c=1
> >>> c and a or b
> 'B'
> >>>
> 
> to avoid this you need
> >>> (c and (a,) or (b,))[0]
> ''

thanks Robin.



More information about the Python-list mailing list