My application reads the python script:
a = MyObject("a")
b = MyObject("b")
my_objects = [a, b]
The following code loads the file and gets my_objects
(exceptions in case of parsing error are omitted):
_config = __import__(file_name)
if hasattr(_config, "my_objects"):
v = getattr(_config, "my_objects")
It works but i want MyObject to have access to another object during file parsing. The only way I found was to declare a global variable in a separate python (globs.py
) file to avoid cyclic import error:
_cached_instance
And then the code of my object is:
import globs
class MyObject(Object):
def __init__(self):
self.cache_instance = globs._cached_instance
It works... but is is not very elegant. The use of a global variable makes testing difficult (and with strange border effect). I'm looking for a more elegant way to "inject" the _cached_instance during the loading of the python script and make it only available in the script. Any ideas ?