reference counter and __del__

shindich at my-deja.com shindich at my-deja.com
Tue Jul 11 00:11:52 EDT 2000


In article <8kdsuj$k9c$1 at nnrp1.deja.com>,
  shindich at my-deja.com wrote:
> In article <3969CB5C.A860D124 at muc.das-werk.de>,
>   thiele at muc.das-werk.de wrote:
> > 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 use sys.getrefcount ().
> Thomas Thiele posted a similar question today, you might want to look
at
> the answers to that posting too. For all we know, there could be a
> reference leak in pickle.
> > 2. When __del__ is called the object will be detroyed, correct?
> Yep. In fact, __del__ is called as the object being destroyed.
> > 3. Why is __init__ not called when I unpickle an object?
> >
> I don't know the exact answer to this question, but my guess is that
it
> doesn't need to call the constructor. I assume that pickle.loads ()
> simple creates an object and sets up the properties on that object to
be
> the same as those of the object that got serialized using
> pickle.dumps().
> But read the docs on it. I am no authority on pickle inetrnals.
<B>You can find the anser at
http://www.python.org/doc/current/lib/module-pickle.html
<i>
The following is an excerpt from that article:
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.
</i>
</B>
> > Thomas
> >
>
> Alex Shindich
> http://www.shindich.com/
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list