if in expression

christian2.schmidt at gmx.de christian2.schmidt at gmx.de
Mon Aug 18 09:46:42 CEST 2008


Hi,
I'm using IronPython to evaluate expressions, so I can't use the
return statement but have to say it in an one-liner. Using C/C++ I
could use "cond ? then : else" to have an expression-if, but in Python
there's no such operator. The "cond and then or else"-trick only seems
to work for non-false "then"s and is not very readable.
So is the pythonic way to def iif(cond, then, else_): if cond: return
then; else: else_; ? But actually this is not the same, because "then"
and "else_" are evaluated independently of cond...

What is the right way to have if in an expression?

Thanks,
Christian



More information about the Python-list mailing list