Amazed as he was, he's now forcing me at gun point to write a post about it!
The first thing you have to understand about Python classes is that all they are is a collection of references to members. Methods are just function pointers that are referenced by the class, not embedded or copied into the class. This is why all methods require that the first parameter be
self, the current instance. Instances themselves contain the real state and reference the class. When you invoke a method on the instance it looks up what function to call.
So if you have two instances of the same class, the methods hanging off them are really the same function.
As they say, the prove is in the pudding.
# A basic class class ClassA: def methodA(self): return True a = ClassA() b = ClassA() # Verify that they have the same function address id(a.methodA) == id(b.methodA)
Following on from the previous snippet let's add, modify and remove references to methods and observe how they impact the instances.
# Let's add a new method setattr(ClassA, "methodB", lambda self : 5) # Verify it exists and returns 5 a.methodB() == 5 b.methodB() == 5 # Lets modify methodA setattr(ClassA, "methodA", lambda self : False) # Let's verify this method now returns False rather than True. a.methodA() == False b.methodA() == False # Let's remove methodA delattr(ClassA, "methodA") # Let's verify it doesn't exist anymore. # Both of these should throw AttributeError a.methodA() b.methodA() # Check methodB still remains. a.methodB() b.methodB()
Fun times. The best way to become boring is to say everything.