i have a question about the next python code.
class A(object):
id = 1
def __init__(self):
self.id = A.id
A.id += 1
def getId(self):
return self.id
def __lt__(self, other):#This method is interested
return self.id < other.id
class B(A):
def __init__(self):
self.id = 1
Then i test it
a1 = A()
a2 = A()
b1 = B()
b2 = B()
print a1.getId(),
print a2.getId(),
print b1.getId(),
print b2.getId(),
print a1.id == a2.id,b1.id == b2.id
and see the result “1 2 1 1 False True”
How to change only the __lt__ – method in A that B instances’ id are different (ie instead of “1 2 1 2 False False” can be seen “False False”)? Is it possible? B must be the same.
I’m going to make a wild guess at what you want to do.
It seems to me that you want “==” to test if two objects are the same objects, as opposed to test it the two objects have the same value.
But you don’t need that. To test if two objects are the same in Python you use the
isoperator. Like so:This way you can use
==to test of the value (ie the id in this case) is the same, while still seeing that it is not the same object.