Python速记条件

Ahm*_*man 17 python conditional if-statement

这是一个快速...

在Python中,可以做到:

foo = foo1 if bar1 else foo2
Run Code Online (Sandbox Code Playgroud)

这很酷,但我怎么能在不写的情况下得到真或假

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

例如,在JS中,您可以通过执行强制转换布尔类型

var foo = !!bar1;
Run Code Online (Sandbox Code Playgroud)