【发布时间】:2015-08-10 10:57:59
【问题描述】:
对于下面定义的用户定义的pythonclass,a == b是False
>>> class Account():
def __init__(self, account_holder):
self.balance = 0
self.holder = account_holder
>>> a = Account('Jim')
>>> b = Account('Jim')
>>> a is b
False
>>> a == b
False
但在以下情况下,相等(==)运算符显示True
>>> lst1 = [1, 2]
>>> lst2 = [1, 2]
>>> lst1 == lst2
True # this is true
>>> lst1 is lst2
False
>>> str1 = 'abc'
>>> str2 = 'abc'
>>> str1 == str2
True # this is true
>>> str1 is str2
True
>>> tup1 = (1, 2)
>>> tup2 = (1, 2)
>>> tup1 == tup2
True # this is true
>>> tup1 is tup2
False
当用户定义的类在 python 中定义时,我如何理解相等运算符 (
==) 的工作原理?class object的哪个方法为 python 中任何用户定义类的所有实例提供标识?
【问题讨论】:
-
我不认为这是身份主题的重复问题
标签: python python-3.x