[Tutor] Are the methods in a class copied or just linked to?
Peter Otten
__peter__ at web.de
Thu Jul 2 23:09:13 CEST 2015
Danny Yoo wrote:
> On Thu, Jul 2, 2015 at 12:30 PM, Jim Mooney Py3.4.3winXP
> <cybervigilante at gmail.com> wrote:
>> When an instance uses a class method, does it actually use the method
>> that is in the class object's memory space, or is the method copied to
>> the instance?
> Unsure. How would it be observable?
[snip implementation details]
My understanding of the question is less involved. I'd suggest:
Create a class Foo with a method bar(), say, instantiate it, replace foo in
the class and then invoke foo in the previously created instance.
If the old method is executed the instance must keep a copy, if the new
method is executed there is probably no copy.
>>> class Foo:
... def bar(self): print("original bar")
...
>>> foo = Foo()
>>> def modified_bar(self):
... print("modified bar")
...
>>> Foo.bar = modified_bar
>>> foo.bar()
modified bar
Conclusion: no copy.
More information about the Tutor
mailing list