For below defined user defined python class
, a == b
is 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
But in below cases, equality(==
) operator shows 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
How do I understand the working of equality operator(
==
), when user defined classes are defined in python?Which method of
class object
provide identity to all instances of any user defined class in python?