Managing global objects with side effects when reloading a module in Python -
Managing global objects with side effects when reloading a module in Python -
i looking way correctly manage module level global variables utilize operating scheme resource (like file or thread).
the problem when module reloaded, resource must disposed (e.g. file closed or thread terminated) before creating new one.
so need improve pattern manage singleton objects.
i've been reading docs on module reload , quite interesting:
when module reloaded, dictionary (containing module’s global variables) retained. redefinitions of names override old definitions, not problem. if new version of module not define name defined old version, old definition remains. feature can used module’s advantage if maintains global table or cache of objects — seek statement can test table’s presence , skip initialization if desired:
try: cache except nameerror: cache = {}
so check if objects exist, , dispose them before creating new ones.
python module side-effects
Comments
Post a Comment