Say I have a class:
class Thing:
i = 0
def __init__(self, a):
self.a = a
def doStuff(self):
print(self.a)
@classmethod
def doClassThings(cls):
cls.i += 1
Then I create a couple of objects from this class:
obj1 = Thing(2)
obj2 = Thing(4)
How many instances of the "doStuff" and "init" methods are there in the program's memory at this time? Are there separate but identical copies of these methods for each of the two objects? Or is there only one "doStuff" method which is shared by both of the thing instances?
For class methods, my understanding is that there is only one instance of each classmethod, and it can be called by writing
Thing.doClassThings()
And that there is also only one "i" variable, shared by the whole class. Is this correct?
Either way, as far as the instance methods go, I don't know how Python works as far as memory use. I really, really don't like the idea of there being copies of these instance methods, because they are identical and this is redundant.
Finally, if Python does create multiple copies of each instance function for every instance of the class, how can I write code that doesn't do this?