python: iif or (x ? a : b)
Possible Duplicate:
Python Ternary Operator
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.
尝试
print ("paid: " + str(paid) + " €") if paid else "not paid"
链接地址: http://www.djcxy.com/p/7400.html
上一篇: 在那儿 '?' Python中的控制流?
下一篇: python:iif或(x?a:b)