- 和C、Java不同,Python中没有NULL,取而代之的是None
- None是一个特殊的常量,表示变量没有指向任何对象
- 在Python中,None本身实际上也是对象,有自己的类型NoneType
- 可以将None赋值给任何变量,但我们不能创建NoneType类型的对象
var = None
var2 = None
print(type(None))
print(id(None))
print(id(var))
print(id(var2))
"""
<class 'NoneType'>
140708184136920
140708184136920
140708184136920
"""
None不是False,None不是0,None不是空字符串。None和任何其他的数据类型比较永远返回False
None和其他类型的比较
a = []
b = ()
c = {}
d = ""
e = 0
f = None
if (not a) and (not b) and (not c) and (not d) and (not e) and (not f):
print("if判断时,空列表、空字符串、0、None等代表空和无的对象会被转换成False")
a = []
b = ()
c = {}
d = ""
e = 0
if (a==False or d==False or b==False or d==False):
print("==时,空列表、空字符串不是False")
if (e==False):
print("==时,0会转成False")