Cannot reference imported module in __del __ ()

I use an object __del__()to cancel it from an event (using an event scheme similar to this ):

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

Oddly enough, I got the following error at the end of the program launch:

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

How is this possible? my_environmentis an imported module, how is it that it can be None? ( eventsis a global object in it with event hooks such as my_event)

+5
source share
2 answers

According to the python doc about__del__ :

[...] , __del__(), , (, ). __del__() , .

, __del__, my_enviroment "" python, None...

+7

Python, , "" . , , , - .

, , , __del__, , , __enter__ __exit__, with. __del__ try-except , - , .

+3

All Articles