[Python-ideas] A "within" keyword

Rhodri James rhodri at kynesim.co.uk
Fri Jun 8 15:01:36 EDT 2018

On 08/06/18 19:41, David Teresi 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"

New keywords have a fairly high barrier to get over.  Do you have a 
convincing use case?  I don't personally consider this particularly 
convincing, not when it's pretty much equivalent to:

 >>> import A
 >>> foo = A.cool_namespace.foo
 >>> foo()

To be honest I wouldn't even bother doing that, I'd just type 
A.cool_namespace.foo() when I wanted it.  Explicit is better than 
implicit, after all.

Rhodri James *-* Kynesim Ltd

More information about the Python-ideas mailing list