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

Samuele Pedroni pedronis@bluewin.ch
Tue, 14 Jan 2003 20:14:12 +0100


From: "Terry Reedy" <tjreedy@udel.edu>
> > 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 may add that Jython already does that, and gets away with it wihout much ado

Jython 2.1 on java1.3.0 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> import __main__
>>> __main__.__dict__[1]=2
Traceback (innermost last):
  File "<console>", line 1, in ?
TypeError: keys in namespace must be strings
>>>

same for class and instances dicts.