Python三元运算符

cyn*_*joy 44 python

可能重复:
Python中的三元条件运算符

var foo = (test) ? "True" : "False";
Run Code Online (Sandbox Code Playgroud)

这在Python中会是什么样子?

使用Python 2.7,如果这有所作为.

Bri*_*nna 89

PEP 308增加了一个三元运算符:

foo = "True" if test else "False"
Run Code Online (Sandbox Code Playgroud)

它已经从Python 2.5开始实现