- if obj:
obj为:None, False, 空字符串’’, 0, 空列表[], 空字典{}, 空元组()时为False,即代表空和无的对象在进行判断时都会被转换成Falsea = [[], '', {}, (), 0, False, None]
for item in a:
print(item, end=' ')
if item:
print("is True")
else:
print("is False")
[] is False
is False
{} is False
() is False
0 is False
False is False
None is False
- if obj is None:
obj为None时为Truea = [[], '', {}, (), 0, False, None]
for item in a:
print(item, end=' ')
if item is None:
print("is None")
else:
print("is not None")
[] is not None
is not None
{} is not None
() is not None
0 is not None
False is not None
None is None
- obj == None:
为True情况:
- obj为None
- 其__eq__方法返回True(此时无论’=='右边为何值比较结果都为True)
class Foo(object):
def __eq__(self, other):
return True
>>> f = Foo()
>>> f == None
True
>>> f is None
False
参考资料:
https://www.cnblogs.com/wangzhao2016/p/6763431.html