Python 2 namespace change? (was Re: [Python-Dev] Changing existing class
instances)
Jim Fulton
jim@digicool.com
Thu, 20 Jan 2000 09:34:13 -0500
Jim Fulton wrote:
>
> Reloading a module redefines the global variables in a module.
> It doesn't update any references to those global references
> from other places, such as instances or *other* modules.
>
> For example, imports like:
>
> from foo import spam
>
> are not updated when foo is reloaded.
A change to the way that namespaces are handled
could make this work and have a number of other benefits,
like global name usage without namespace lookups.
I've suggested this to Guido in the past. His
reasonable response is that this would be too big a
change for Python 1. Maybe this is something to consider
for Python 2?
The basic idea (borrowed from Smalltalk) is to have a kind
of dictionary that is a collection of "association"
objects. An association object is simply a pairing of a
name with a value. Association objects can be shared among
multiple namespaces. An import like:
from foo import spam
would copy the association between the name 'foo' and a
value from module 'spam' into the current module. If foo
is reloaded or if the name is reassigned in spam, the
association is modified and the change is seen in any
namespaces that imported foo.
Similarly if a function uses a global variable:
spam=1
def bar():
global spam
return spam*2
the compiled function contains the association between
spam and it's value. This means that:
- When spam is used in the function, it doesn't have to
be looked up,
- The function object no longer needs to keep a reference
to it's globals. This eliminates an annoying circular
reference.
(I would not replace existing dictionaries with this new kind.
I'd have both kinds available.)
I think that this would be a really nice change for Python 2.
Jim
--
Jim Fulton mailto:jim@digicool.com
Technical Director (888) 344-4332 Python Powered!
Digital Creations http://www.digicool.com http://www.python.org
Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email
address may not be added to any commercial mail list with out my
permission. Violation of my privacy with advertising or SPAM will
result in a suit for a MINIMUM of $500 damages/incident, $1500 for
repeats.