Why instance methods point to the same address but are not the same object?

Viewed 36

I have the following minimal code which defines a class and two class objects:

class A():
    def instance_method(self):
        pass

a1 = A()
a2 = A()

a1 and a2 does not appear to share the same instance method, which makes sense to me.

assert a1.instance_method is not a2.instance_method

However, when I checked the ID of the two methods, they appear identical.

assert id(a1.instance_method) == id(a2.instance_method)

So I am a bit confused about whether or not instances share an instance method.. Anyone can shed a light on what's going on here in memory? Thanks.

0 Answers
Related