这里主要讲述了 python 中 ==
和 is
的异同。
参考资料
解析
推荐你看我之前写的博文。
不可变对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| a = 1000 b = 1000 print(a is b) print(a == b)
a = '哈哈' b = '哈哈' print(a is b) print(a == b)
a = (1, 2, 3) b = (1, 2, 3) print(a is b) print(a == b)
|
可变对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| a = {'h': 1} b = {'h': 1} print(a is b) print(a == b)
a = {1, 2, 3} b = {1, 2, 3} print(a is b) print(a == b)
class Node: def __init__(self, x): self.val = x self.next = None
n1 = Node(1) n2 = Node(1) print(a is b) print(a == b)
|