I am reading about classes in Python (3.4) and from what I understand it seems that every new object has its own bound methods instances.
class A:
def __init__(self, name):
self.name = name
def foo(self):
print(self.name)
a = A('One')
b = A('Two')
print(a.foo == b.foo)
The output of this is False
.
This seems to me as a waste of memory. I thought that internally a.foo
and b.foo
would point somehow internally to one function in memory: A.foo
where self
as the class instance will be passed.
I assume this maybe cannot be implemented easily in the language.
Does each new instance contain also new instances of its bound methods?
If so, does not this hurt the performance or make case for creating new objects more cautiously than in other languages where methods are "shared" among objects like in Java?
See Question&Answers more detail:os