<div dir="ltr">You can use ``eval`` to run an expression, swapping in a different globals and/or locals namespace. Will this serve your purpose?<div><br></div><div><div><div><span style="white-space:pre"> </span>In [1]: import types</div><div><span style="white-space:pre"> </span>In [2]: ns = types.SimpleNamespace(a=1)</div><div><span style="white-space:pre"> </span>In [3]: eval('a', ns.__dict__)</div><div><span style="white-space:pre"> </span>Out[3]: 1</div></div><div><br></div><div><a href="https://docs.python.org/3/library/functions.html#eval">https://docs.python.org/3/library/functions.html#eval</a><br><br><div class="gmail_quote"><div dir="ltr">On Fri, Jun 8, 2018 at 11:43 AM David Teresi <<a href="mailto:dkteresi@gmail.com">dkteresi@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>One of the features I miss from languages such as C# is namespaces that work across files - it makes it a lot easier to organize code IMO.</div><div><br></div><div>Here's an idea I had - it might not be the best idea, just throwing this out there: a "within" keyword that lets you execute code inside a namespace. For example:</div><div><br></div><div><font face="monospace,monospace"># A.py</font></div><div><font face="monospace,monospace">import types</font></div><div><font face="monospace,monospace">cool_namespace = types.SimpleNamespace()</font></div><div><font face="monospace,monospace"><br></font></div><div><font face="monospace,monospace">within cool_namespace:</font></div><div><font face="monospace,monospace"> def foo():</font></div><div><font face="monospace,monospace"> print("foo run")</font></div><div><font face="monospace,monospace"><br></font></div><div><font face="monospace,monospace">#B.py</font></div><div><font face="monospace,monospace">import A</font></div><div><font face="monospace,monospace">within A.cool_namespace:</font></div><div><font face="monospace,monospace"> foo() # prints "foo run"</font></div><div><font face="monospace,monospace"><br></font></div><div><font face="monospace,monospace"><font face="arial,helvetica,sans-serif">Thoughts?</font></font><br></div></div>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div></div></div></div>