Python 等价于设置 if not null [英] Python equivalent of setting if not null
本文介绍了Python 等价于设置 if not null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 ruby 中,我可以做到这一点:
In ruby I can do this:
1.9.3-p448 :001 > a = 1 || 2
=> 1
1.9.3-p448 :004 > a = nil || 2
=> 2
1.9.3-p448 :005 > a = 1 || nil
=> 1
Python 中是否有类似的单行代码?
Is there a similar one-liner in Python?
推荐答案
只需使用 or
运算符.从链接页面:
Just use the or
operator. From the linked page:
x 或 y:如果 x 为假,则为 y,否则为 x
x or y: if x is false, then y, else x
示例:
In [1]: 1 or 2
Out[1]: 1
In [2]: None or 2
Out[2]: 2
In [3]: 1 or None
Out[3]: 1
这篇关于Python 等价于设置 if not null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文