[Python-ideas] A "within" keyword

Alex Walters tritium-list at sdamon.com
Fri Jun 8 23:27:58 EDT 2018



> -----Original Message-----
> From: Python-ideas <python-ideas-bounces+tritium-
> list=sdamon.com at python.org> On Behalf Of David Teresi
> Sent: Friday, June 8, 2018 2:42 PM
> To: python-ideas at python.org
> Subject: [Python-ideas] A "within" keyword
> 
> 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?

Why not...

cool_namespace = SomeNamespaceContextManager()

with cool_namespace:
    def foo():
        pass

advantage being it introduces no new keyword.  The 'disadvantage' is it would change semantics of the with statement (as would be required to get the names defined in the suite of the context manager)




More information about the Python-ideas mailing list