None 表示的是一个对象,定义为 Nonetype 的对象。
A is None 表示的是 A 的内存地址 与 None 一致,也就是说,如果A是一个对象但是对象属性定义为None的话,依然返回False。因为有对象,即有对应的地址空间。
如果 A是一个对象的属性(变量),A的值设定为None,则is None返回True。
A==B 表示 对象A和B相等,相当于调用了__eq()__方法。当A B都为None时,自然返回True。
这里以linkedlist作为实例
class Node:
def __init__(self,value=None,next=None):
self.value = value
self.next = next
def getNode(self):
return self.value
def getNext(self):
return self.next
def setValue(self,new_value):
self.value = new_value
def setNext(self,new_next):
self.next = new_next
class LinkedList:
def __init__(self):
self.head = Node() #important. head is node not attribute.
self.tail = None
self.length = 0
def isEmpty(self):
print(self.head)#object is not None
print(self.head.value) # value is None
return self.head.value == None #thus if there is no node,return True.
PS:
if Object
对象的判断,都返回True,其中None,False,0,[],"",{},()都相当于False,即not None == not False == not ‘’ == not 0 == not [] == not {} == not () 均为True。