if in expression

John Machin sjmachin at lexicon.net
Mon Aug 18 04:22:26 EDT 2008


On Aug 18, 5:46 pm, christian2.schm... at gmx.de wrote:
> 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.

By "evaluate expressions", do you mean using the eval built-in
function? If so, find the recent thread addressing this topic; it was
asserted that 98% of eval() use-cases were better implemented another
way. What's _your_ use-case?

> 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.

There is an even more unreadable hack (due to Tim Peters IIRC) that
avoides the false-then problem:

(cond and [then_value] or [else_value])[0]




More information about the Python-list mailing list