Getters and Setters

Bernhard Herzog herzog at online.de
Thu Jul 15 14:00:18 EDT 1999


"Tim Peters" <tim_one at email.msn.com> writes:

> [Neil Schemenauer]
..
> > ...
> > Your code is fast and elegant but it causes reference loops.
> 
> ?  Not under my Python <wink> -- stick a __del__ method in the test class
> that prints something and you'll see that objects go away when you expect.
> The function object assigned to self.set/get_XXX contains one ref to
> self.__dict__ and another to the attr's string name, and that's it; it is
> *not* a bound method object (which would also hold a reference to self),
> just a function object.  So there's no cycle unless you force one, like
> 
>     instance.setWhatever(instance)
> 
> But then you would have had a cycle without this code too.

The cycle doesn't involve self, but self's dict will hold an indirect
reference to itself. The instance object will be deleted, so it's
__del__ method is called, but its __dict__ won't be deleted:

Python 1.5.2 (#3, Apr 21 1999, 16:51:54)  [GCC 2.7.2.3] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> class C:
..     def __del__(self):
..             print '*del*'
.. 
>>> c = C()
>>> c.x = C()
>>> del c
*del*
*del*
>>> c = C()
>>> c.x = C()
>>> c.y = c.__dict__
>>> del c
*del*
>>> 



-- 
Bernhard Herzog	  | Sketch, a python based drawing program
herzog at online.de  | http://www.online.de/home/sketch/




More information about the Python-list mailing list