<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to share living instance?]]></title><description><![CDATA[<p dir="auto">Hi,<br />
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.</p>
<pre><code class="language-python">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
</code></pre>
<p dir="auto">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.</p>
<p dir="auto">This is another approach when setting it into the main file</p>
<pre><code class="language-python">import __main__ 
setattr(__main__, 'testjeee', 30)
</code></pre>
<p dir="auto">that can be later accessed after importing <code>__main__</code>.</p>
<p dir="auto">Is there maybe a recommended way how to push it to RoboFont's globals? Or is the second safe enough? Thanks!</p>
]]></description><link>https://forum.robofont.com/topic/833/how-to-share-living-instance</link><generator>RSS for Node</generator><lastBuildDate>Thu, 14 May 2026 04:04:59 GMT</lastBuildDate><atom:link href="https://forum.robofont.com/topic/833.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 08 Apr 2020 23:02:11 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to share living instance? on Thu, 09 Apr 2020 11:53:38 GMT]]></title><description><![CDATA[<p dir="auto">Hi,<br />
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.</p>
<pre><code class="language-python">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
</code></pre>
<p dir="auto">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.</p>
<p dir="auto">This is another approach when setting it into the main file</p>
<pre><code class="language-python">import __main__ 
setattr(__main__, 'testjeee', 30)
</code></pre>
<p dir="auto">that can be later accessed after importing <code>__main__</code>.</p>
<p dir="auto">Is there maybe a recommended way how to push it to RoboFont's globals? Or is the second safe enough? Thanks!</p>
]]></description><link>https://forum.robofont.com/post/3169</link><guid isPermaLink="true">https://forum.robofont.com/post/3169</guid><dc:creator><![CDATA[jansindl3r]]></dc:creator><pubDate>Thu, 09 Apr 2020 11:53:38 GMT</pubDate></item><item><title><![CDATA[Reply to How to share living instance? on Fri, 10 Apr 2020 09:57:20 GMT]]></title><description><![CDATA[<p dir="auto">hello <a class="plugin-mentions-user plugin-mentions-a" href="https://forum.robofont.com/uid/454">@jansindl3r</a>,</p>
<p dir="auto">not sure I understand the question…</p>
<p dir="auto">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:</p>
<pre><code class="language-python"># myModule.py
class MyClass:
    pass
myInstance = MyClass()
</code></pre>
<pre><code class="language-python"># myScript.py
import myModule
print(myModule.myInstance)
</code></pre>
<pre><code class="language-console">&lt;myModule.MyClass object at 0x1248ee910&gt;
</code></pre>
<p dir="auto">if this is not working for you, try reloading the module:</p>
<pre><code class="language-python">from importlib import reload
import myModule
reload(myModule)
</code></pre>
<p dir="auto">btw <code>gc</code> is an <a href="https://docs.python.org/3/library/gc.html" rel="nofollow">existing module</a> in Python (Garbage Collector), this may lead to conflicts.</p>
<p dir="auto">hope this makes sense! cheers</p>
]]></description><link>https://forum.robofont.com/post/3178</link><guid isPermaLink="true">https://forum.robofont.com/post/3178</guid><dc:creator><![CDATA[gferreira]]></dc:creator><pubDate>Fri, 10 Apr 2020 09:57:20 GMT</pubDate></item><item><title><![CDATA[Reply to How to share living instance? on Fri, 10 Apr 2020 13:08:52 GMT]]></title><description><![CDATA[<p dir="auto">hi, <a class="plugin-mentions-user plugin-mentions-a" href="https://forum.robofont.com/uid/22">@gferreira</a>!<br />
Yes I was using gc to get the instance, but it seems too risky.<br />
I went for the <code>__main__</code> approach.</p>
<p dir="auto">Well, this doesn't work for me. It import's the class but not the latest instance.</p>
<p dir="auto">address of the latest instance that I am interested in is <code>4551262352</code><br />
while imported's address is <code>4609259344</code> so those are two different things.<br />
I doubt that it be so easy while such things are solved through socket communication, aren't they?<br />
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 :/ "</p>
<p dir="auto">so I have collected 3 approaches</p>
<ul>
<li>searching in gc, either based on address or name = dangerous</li>
<li>setting link through <code>__main__</code> = fine</li>
<li>setting up protocol communication = too much work</li>
</ul>
<p dir="auto">I will go for the second one<br />
thanks, Jan</p>
]]></description><link>https://forum.robofont.com/post/3182</link><guid isPermaLink="true">https://forum.robofont.com/post/3182</guid><dc:creator><![CDATA[jansindl3r]]></dc:creator><pubDate>Fri, 10 Apr 2020 13:08:52 GMT</pubDate></item><item><title><![CDATA[Reply to How to share living instance? on Fri, 10 Apr 2020 13:15:59 GMT]]></title><description><![CDATA[<p dir="auto">Dont get it why Gustavo's answer does not work...</p>
<p dir="auto">You initiate only once an instance from that class and import it back into other modules.</p>
<p dir="auto">A fourth easy solution is to add a variable to <code>NSApp()</code></p>
<pre><code>import AppKit

app = AppKit.NSApp()
app._my_extenstion_name_controller = "foo"
</code></pre>
<p dir="auto">this will be accessible everywhere, but this is actually the same as Gustavo's answers, just stored somewhere else :)</p>
]]></description><link>https://forum.robofont.com/post/3183</link><guid isPermaLink="true">https://forum.robofont.com/post/3183</guid><dc:creator><![CDATA[frederik]]></dc:creator><pubDate>Fri, 10 Apr 2020 13:15:59 GMT</pubDate></item></channel></rss>