简单来讲
tmpName = ‘’
if tmpName:
print tmpName
#没有输出
if tmpName is not None:
print tmpName
#有输出,是空行
代码中一般有三种方式判断变量是否为None:
(1)x = None的情况
x = None
if x :
print("if x ") # 此时无打印结果
if x is not None:
print("if x is not None")# 此时打印结果为空
此时如果是bool(x)的话,
>>> bool(x)
False
(2)x = []
x = None
if x :
print("if x ") # 此时无打印结果
if x is not None:
print("if x is not None")# 此时打印结果为 if x is not None
此时如果是bool(x)的话,
>>> bool(x)
False
(3)x = 12
x = 12
if x :
print("if x ") # 此时打印结果为:if x
if x is not None:
print("if x is not None")# 此时打印结果为:if x is not None
此时如果是bool(x)的话,
>>> bool(x)
Ture