[Python-Dev] Re: Re: PEP 267 improvement idea

Jack Diederich jack@performancedrivers.com
Tue, 14 Jan 2003 14:31:21 -0500


On Tue, Jan 14, 2003 at 01:44:37PM -0500, Terry Reedy wrote:
> > overhead pays when it's used as a namespace. Both
> "lookdict_namespace"
> > and "lookdict_string" will fall back to a "lookdict" dictionary when
> a
> > non-string key is inserted.
> 
> Until reading this sentence, I had never thought of doing something so
> bizarre as adding a non-string key to a namespace (via the 'backdoor'
> .__dict__ reference).  But it currently works.
> 
> >>> import __main__
> >>> __main__.__dict__[1]='x'
> >>> dir()
> [1, '__builtins__', '__doc__', '__main__', '__name__', 'x']
> 
> However, since the implementation of namespaces is an implementation
> detail, and since I can (as yet) see no need for the above (except to
> astound and amaze), I think you (and Guido) should feel free to
> disallow this, especially if doing so facilitates speed improvements.

I think namespaces should be thier own type, even if name only
so all those PEPs will have a drop-in place to experiment.
The fact that namespaces are implemented with regular dicts is
incidental.

I have a couple experiments I'd love to do, but currently fooling with
namespaces is very touchy.  Granted if you fscked up the namespace type
it would be hard to debug, but at least you would know it was namespace
and not a side effect of breaking dict as well.

The intial definition could be a string-key only dict

down the road the following code:

import mymodule
foo = 7

could generate the following events

namespace = __builtins__ # init
namespace['mymodule.'] = mymodule.__dict__ # import 'mymodule'
namespace['foo'] = 7 # create and assign local variable 'foo'


-jackdied