https://book.douban.com/subje…
C?X:Y
,它表示条件C为真时,取X值,否则,取Y值。
In [20]: a=1
In [21]: b=2
In [22]: a if a>b else b
Out[22]: 2
In [23]: y= a if a>b else b
In [24]: y
Out[24]: 2
python的替代方案
In [18]: def f(x):
...: return {
...: 0:\"you zero.\\n\",
...: 1:\"top.\\n\",
...: 2:\"n is last\"
...: }.get(x,\"only number\\n\")
上一篇:python的装饰器入门小结
下一篇:django权限的自定义