I was experimenting with class method in python when I stumbled upon this issue of id.
class MyClass:
def method(self):
return 'instance method called', self
@classmethod
def classmethod(cls):
return 'class method called', cls
@staticmethod
def staticmethod():
return 'static method called'
print(id(MyClass.classmethod), "MyClass")
print(id(obj1.classmethod), "obj1")
print(id(obj2.classmethod), "obj2")
print("------------------------")
print(id(MyClass.classmethod), MyClass.classmethod)
print(id(obj1.classmethod), obj1.classmethod)
print(id(obj2.classmethod), obj2.classmethod)
The output is Output
140600363181376 MyClass
140600363181568 Obj1
140600363181376 Obj2
------------------------
140600363181568 <bound method MyClass.classmethod of <class '__main__.MyClass'>>
140600363181568 <bound method MyClass.classmethod of <class '__main__.MyClass'>>
140600363181568 <bound method MyClass.classmethod of <class '__main__.MyClass'>>
Why is the id different though the same methods were called?
When using python 3.9.12 and 3.10.7, the output was similar as in above. But when using python 3.8.10, the print of all id was same.