Jonathan Jonathan - 1 year ago 36
Python Question

Unable to reference an imported module in __del__()

I'm using an object's

to unsubscribe it from an event (using an event scheme similar to this):

import my_enviroment
class MyClass():
def __del__(self): -= self.event_handler_func

Oddly I received the following error at the end of the program's run:

Exception AttributeError: "'NoneType' object has no attribute 'events'" in <bound method MyClass.__del__ of <myclass.MyClass instance at 0x04C54580>> ignored

How could this be possible?!
is a module I imported, how come it could be None? (
is a global object in it with event hooks such as


According to the python doc about __del__ :

[...] other globals referenced by the __del__() method may already have been deleted or in the process of being torn down (e.g. the import machinery shutting down). For this reason, __del__() methods should do the absolute minimum needed to maintain external invariants.

In other words, when the __del__ method is called on your object, the my_enviroment may have been 'deleted' by python, so it can be None...