delete and circular references
Paul-Michael Agapow
p.agapow at ic.ac.uk
Thu Oct 26 10:26:20 EDT 2000
I can't find an answer to this in the documentation so: what's the best
way to handle circular references with an object that is about to be
deleted? It is unclear to me exactly what __del__ () does, whether it is
a deletion or pre-deletion handler.
For example: I have a class that is a tree data structure. As I wish to
traverse up and down the branches fast, the nodes contain references to
their parent and their child nodes. Hence the circularity:
class node:
def __init__ (self):
parent = None
children = []
# etc. etc.
class tree:
def __init__ (self, treename):
theNodes = []
theName = treename
theRoot = None
# etc. etc.
When the tree (or any individual node) is being deleted, the circular
references must be broken. So do I break the links in __del__()?
def __del__ (self)
for node in theNodes:
node.parent = node.children = None
If so, do I then have to write as well
del theName
del theRoot
del theNodes
or is __del__ just for pre-deletion manipulations (the system handling
the second part of the function itself)?
--
Paul-Michael Agapow (p.agapow at ic.ac.uk), Biology, Imperial College
"Pikachu's special power is that he is monophyletic with lagomorphs ..."
More information about the Python-list
mailing list