python: iif or (x ? a : b)

Posted by Albert on Stack Overflow See other posts from Stack Overflow or by Albert
Published on 2010-06-15T13:40:04Z Indexed on 2010/06/15 13:42 UTC
Read the original article Hit count: 365

Filed under:
|

If Python would support the (x ? a : b) syntax from C/C++, I would write:

print paid ? ("paid: " + str(paid) + " €") : "not paid"

I really don't want to have an if-check and two independent prints here (because that is only an example above, in my code, it looks much more complicated and would really be stupid to have almost the same code twice).

However, Python does not support this operator or any similar operator (afaik). What is the easiest/cleanest/most common way to do this?

I have searched a bit and seen someone defining an iif(cond,iftrue,iffalse) function, inspired from Visual Basic. I wondered if I really have to add that code and if/why there is no such basic function in the standard library.

© Stack Overflow or respective owner

Related posts about python

Related posts about iif-function