__del__ and reference count problem

Fredrik Lundh fredrik at pythonware.com
Thu Apr 21 14:30:56 EDT 2005


ajikoe at gmail.com wrote:

> Your problem can be simplified :
> 
> class A:
>  pop = 11
>  def __del__(self):
>    print A.pop
> 
> if __name__ == '__main__':
>  objA = A()
> 
> Exception exceptions.AttributeError: "'NoneType' object has no
> attribute 'pop'" in <bound method A.__del__ of <__main__.A instance at
> 0x01474A08>> ignored
> 
> I got the same error message, and I don't know why ? it looks like the
> class variable can't be accessed from __del__?

when Python shuts down, modules are sometimes cleaned out before objects
created from classes in those modules.  if you really need to access a global
object during finalization, you have to hold on to it yourself.

    def __del__(self, A=A):
        print A.pop

(the best solution is to avoid using finalizers, of course, but that's another story)

</F>




More information about the Python-list mailing list