reference counter and __del__

Alex cut_me_out at hotmail.com
Mon Jul 10 09:23:07 EDT 2000


> 1. I have the problem that not all of my objects are destroyed and I
> don't know where and why, so I wonder if there a way to check out from
> where and how many references to a special object exists? 

You can find out how many using sys.getrefcount.  There's a module by
Tim Peters, plumbo.py, that can help you find cyclic references.  I
found it a little tricky to use, though.  

> 2. When __del__ is called the object will be detroyed, correct?

I think that's right.

> 3. Why is __init__ not called when I unpickle an object? 

I guess it's assumed that it was initialized prior to the pickling.  If
you want to re-initialize it, check out the __getinitargs__ method, at
http://www.python.org/doc/current/lib/module-pickle.html:

> When a pickled class instance is unpickled, its __init__() method is
> normally not invoked. Note: This is a deviation from previous versions
> of this module; the change was introduced in Python 1.5b2. The reason
> for the change is that in many cases it is desirable to have a
> constructor that requires arguments; it is a (minor) ;nuisance to have
> to provide a __getinitargs__() method.
> 
> If it is desirable that the __init__() method be called on unpickling,
> a class can define a method __getinitargs__(), which should return a
> tuple containing the arguments to be passed to the class constructor
> (__init__()). This method is called at pickle time; the tuple it
> returns is incorporated in the pickle for the instance.

Alex.



More information about the Python-list mailing list