Reference Cycles with instance method

A

Amit Dev

Simple question. If I have the following code:

class A:
def __init__(self, s):
self.s = s
self.m2 = m1

def m1(self):
pass

if __name__ == '__main__':
a = A("ads")
a.m1()
a = None

The object is not garbage collected, since there appears to be a cycle
(between method m2 and A). I would expect this to behave the same as
having another method "def m2(self): self.m1()", but unfortunately its
not.
In above case m2 seems to be in a.__dict__ which is causing the cycle.
Any idea why this is so?

Regards,
Amit
 
P

Peter Otten

Amit said:
Simple question. If I have the following code:

class A:
def __init__(self, s):
self.s = s
self.m2 = m1

def m1(self):
pass

if __name__ == '__main__':
a = A("ads")
a.m1()
a = None

The object is not garbage collected, since there appears to be a cycle
(between method m2 and A). I would expect this to behave the same as
having another method "def m2(self): self.m1()", but unfortunately its
not.
In above case m2 seems to be in a.__dict__ which is causing the cycle.
Any idea why this is so?

Hm, I get a NameError

Traceback (most recent call last):
File "tmp.py", line 10, in <module>
a = A("ads")
File "tmp.py", line 4, in __init__
self.m2 = m1
NameError: global name 'm1' is not defined

In general self.some_method returns a bound method which holds a reference
to both the some_method function and the instance:
.... def m(self): print "M"
....(<__main__.A instance at 0x7f91c040e638>, <function m at 0x7f91c03f8b18>)

Therefore something like a.mm = a.m will create a reference cycle.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top