[Python-ideas] A "within" keyword
Michael Selik
mike at selik.org
Fri Jun 8 18:07:28 EDT 2018
You can use ``eval`` to run an expression, swapping in a different globals
and/or locals namespace. Will this serve your purpose?
In [1]: import types
In [2]: ns = types.SimpleNamespace(a=1)
In [3]: eval('a', ns.__dict__)
Out[3]: 1
https://docs.python.org/3/library/functions.html#eval
On Fri, Jun 8, 2018 at 11:43 AM David Teresi <dkteresi at gmail.com> wrote:
> 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.
>
> 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:
>
> # A.py
> import types
> cool_namespace = types.SimpleNamespace()
>
> within cool_namespace:
> def foo():
> print("foo run")
>
> #B.py
> import A
> within A.cool_namespace:
> foo() # prints "foo run"
>
> Thoughts?
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180608/e031398d/attachment-0001.html>
More information about the Python-ideas
mailing list