UNSOLVED How to share living instance?
jansindl3r last edited by gferreira
I have this extension and I would like to share its instance for other parts of the extension so they can operate it from separate files.
import gc def objectByClass(searchedClass): for obj in gc.get_objects(): if obj.__class__.__name__ == searchedClass.__name__: return obj else: print('not found') return None
This works, but looks and feels a bit scary. as I am not really that experienced, I can't say when it can go wrong. It finds living instance of a observing class.
This is another approach when setting it into the main file
import __main__ setattr(__main__, 'testjeee', 30)
that can be later accessed after importing
Is there maybe a recommended way how to push it to RoboFont's globals? Or is the second safe enough? Thanks!
Dont get it why Gustavo's answer does not work...
You initiate only once an instance from that class and import it back into other modules.
A fourth easy solution is to add a variable to
import AppKit app = AppKit.NSApp() app._my_extenstion_name_controller = "foo"
this will be accessible everywhere, but this is actually the same as Gustavo's answers, just stored somewhere else :)
jansindl3r last edited by
Yes I was using gc to get the instance, but it seems too risky.
I went for the
Well, this doesn't work for me. It import's the class but not the latest instance.
address of the latest instance that I am interested in is
while imported's address is
4609259344so those are two different things.
I doubt that it be so easy while such things are solved through socket communication, aren't they?
The approach that you described would work if the second file initialized the first imported one, which it can't. "I think - I am not sure :/ "
so I have collected 3 approaches
- searching in gc, either based on address or name = dangerous
- setting link through
- setting up protocol communication = too much work
I will go for the second one
gferreira last edited by
not sure I understand the question…
if you create an instance in a module and assign it to a variable, it becomes available in the module’s namespace like any other object:
# myModule.py class MyClass: pass myInstance = MyClass()
# myScript.py import myModule print(myModule.myInstance)
<myModule.MyClass object at 0x1248ee910>
if this is not working for you, try reloading the module:
from importlib import reload import myModule reload(myModule)
gcis an existing module in Python (Garbage Collector), this may lead to conflicts.
hope this makes sense! cheers