stumbled on python iif() equivalent, at last
Robin Becker
robin at jessikat.fsnet.co.uk
Sat Jun 23 12:05:49 EDT 2001
In article <3B34B8DE.67D97C24 at q-survey.be>, Laurent Szyster
<laurent.szyster at q-survey.be> writes
>as i was peeking into compileall.py, i stumbled on:
>
> cfile = fullname + (__debug__ and 'c' or 'o')
>
>surprise:
>
> Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license" for more information.
> IDLE 0.6 -- press F1 for help
> >>> __debug__ and 'c' or 'o'
> 'c'
>
>so, there's an iif (condition, is_true, is_false) python equivalent:
>
> >>> 1 and 'c' or 'o'
> 'c'
> >>> 0 and 'c' or 'o'
> 'c'
>
>nice-to-have for one-liner :-)
>
>
>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]
''
>>>
--
Robin Becker
More information about the Python-list
mailing list