Parent references: hot or not?
Andrew Bennetts
andrew-pythonlist at puzzling.org
Sat Apr 19 13:10:22 EDT 2003
On Sat, Apr 19, 2003 at 10:07:41AM -0700, Dylan Reinhardt wrote:
> On Sat, 2003-04-19 at 10:00, Dylan Reinhardt wrote:
>
> > Sounds like I should be sure to write a __del__ method for Container
> > that ensures things work correctly.
>
> And upon re-reading your post you say *not* to do that... :-)
>
> I'd like to understand that a bit better. What would be wrong with:
>
> class Container:
>
> def __del__(self):
> # ensure all Items are deleted first
> for item in self.items.keys():
> del self.items[item]
Calling self.items.clear() would be simpler than looping.
But it's redundant. The garbage collector will collect cycles for you
automatically, so don't waste your time doing unnecessary house-keeping --
particularly because if you *do* define __del__, but somehow accidentally
end out with that object in a reference cycle anyway, it can't be collected.
See http://python.org/doc/current/lib/module-gc.html#l2h-312 for a brief
explanation.
There are very few reasons to define __del__ in modern versions of Python.
-Andrew.
More information about the Python-list
mailing list