python's newbie question

Fredrik Lundh fredrik at pythonware.com
Thu Oct 12 05:38:21 EDT 2006


tpochep at mail.ru wrote:

> begin...
> Derived.__init__:
> Base1.__init__ <__main__.Derived instance at 0x1869adcc>
> Base2.__init__ <__main__.Derived instance at 0x1869adcc>
> end...
> Derived.__del__:
> Exception exceptions.AttributeError: "'NoneType' object has no
> attribute '__del__'" in <bound method
> Derived.__del__ of <__main__.Derived instance at 0x1869adcc>> ignored
>
> I tried different names instead of b1, sometimes programm works,
> sometimes I have an exception. So it's clear, that I've done some
> stupid mistake, but it's not clear for me, python's newbie, where :)

The real mistake here is to use __del__ in your program; don't do that, unless
you have really good reasons (and they are few and not very common).  In most
cases, you can just rely on Python's garbage collector; it'll clean up after you all
by itself, most of the time.

The error you're seeing is due to the fact that Python attempts to destroy every-
thing during shutdown, and this process includes clearing out the contents of all
modules in the program.  Your object is simply destroyed *after* the module it's
defined in is cleared.  To work around this, you need to keep references to all
objects you might end up needing in your object.  (Or just drop the __del__
methods.  Chances are that you don't really need them.)  For more on module
cleanup, see this old Guido essay:

    http://www.python.org/doc/essays/cleanup/

</F> 






More information about the Python-list mailing list