且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

if <obj> 之间的区别如果 <obj>不是无

更新时间:2022-06-10 05:45:38

if obj is not None 测试对象是否不是 None.if obj 测试 bool(obj) 是否为 True.

if obj is not None test whether the object is not None. if obj tests whether bool(obj) is True.

有许多对象不是 None 而是 bool(obj) 为 False:例如,一个空列表、一个空字典、一个空集、一个空字符串...

There are many objects which are not None but for which bool(obj) is False: for instance, an empty list, an empty dict, an empty set, an empty string. . .

当你想测试一个对象是否不是 None 时,使用 if obj is not None.if obj 仅在您想测试一般虚假"时使用 - 其定义是依赖于对象的.

Use if obj is not None when you want to test if an object is not None. Use if obj only if you want to test for general "falseness" -- whose definition is object-dependent.