I would like to ask if using a delegation template in Python would result in circular references, and if so, what would be the best way to implement it to ensure that the object and its delegate are garbage collected?
In Objective-C, the above problem is avoided by using a weak delegate reference. In C ++, we do not call delete on the delegate. I found a link to the weak Python reference module here: http://docs.python.org/library/weakref.html . It seems like a plausible approach could be to create a weak link to the instance variable link using this module, but I'm not sure.
Since I asked this question and could not find answers to it, I am wondering if this is even a problem in Python or if there is a general solution (without the weakref module) that I donβt know? Also, I looked at stackoverflow first, but the questions that I found relate to round-robin import or delegation of the template as a whole and are not specific to Python and the circular link problem.
Thanks in advance for any answers.
Below is some code for a toy example that will help illustrate my question. I implemented the code this way and it works, but I'm not sure if the memory is garbage collected at the end.
class A(object): def __init__(self): self.delegate = None
EDIT
After reading some answers, I decided to clarify my question. I understand that Python has garbage collection. I was not sure if it would garbage collect on the circular referenced objects. My worries come from the following passage from a Python document:
Implementation details of CPython: CPython currently uses a link counting scheme with an (optionally) delayed detection of cyclically related garbage that collects most objects as soon as they become unavailable, but garbage collection containing round links is not guaranteed . See the gc module documentation for information on controlling cyclic garbage collection. Other implementations act differently, and CPython may change. Do not depend on the immediate completion of objects when they become inaccessible (for example: always close files).
Walkthrough in its original form can be found here: http://docs.python.org/reference/datamodel.html Bold is mine.
The following post gives a clearer explanation of the problem of circular reference objects and why this will prevent garbage collection on these objects (at least in a typical setup): http://www.electricmonk.nl/log/2008/07/07/python- destructor-and-garbage-collection-notes / .
Also, I just stumbled upon Alex Martellli, answering a question about whether Python users should worry about circular reference: Should I worry about circular references to Python? From his answer, I understand that although the round objects referenced will eventually collect garbage, there will be overhead. Does this depend heavily on the program.
He also mentioned the use of the Python weakref module, but clearly did not say how to do this.
Therefore, I would like to add the following questions in order to clarify some unresolved issues:
- Documents say the garbage collector is not guaranteed for circular referencing objects. But from the answers it seems that this is not the case. So I misunderstood this passage or the details that I missed?
- I believe, using a weak link, as indicated in the answer of Alex and my question, will it completely eliminate the problems associated with overhead?
Thanks again for the answers.