Class Variable Access and Assignment

Christopher Subich csubich.spam.block at spam.subich.block.com
Fri Nov 4 14:46:39 EST 2005


Bengt Richter wrote:
> 
> It might be interesting to have a means to push and pop objects
> onto/off-of a name-space-shadowing stack (__nsstack__), such that the first place
> to look up a bare name would be as an attribute of the top stack object, i.e.,
>    
>     name = name + 1
> 

Don't be that specific; just unify Attributes and Names.

Instead of the 'name' X referring to locals()['X'] or globals()['X'], 
have a hidden "namespace" object/"class", with lookups functioning akin 
to class inheritence.

This would allow, in theory, more uniform namespace behaviour with outer 
scoping:

x = 1
def f():
    x += 1 # would work, as it becomes 
setattr(namespace,'x',getattr(namespace,'x')+1), just like attribute loookup

Also, with a new keyword "outer", more rational closures would work:

def makeincr(start=0):
    i = start
    def inc():
       outer i
       j = i
       i += 1
       return j
    return inc

 From a "namespace object" point of view, 'outer i' would declare i to 
be a descriptor on the namespace object, such that setting actions would 
set the variable in the inherited scope (getting actions wouldn't 
actually need modification, since it already falls-through).  At the 
first level, 'outer' would be exactly the same as 'global' -- indeed, it 
would be reasonable for the outer keyword to entirely replace global 
(which is actually module-scope).

As it stands, the different behaviours of names and attributes is only a 
minor quirk, and the fix would definitely break backwards compatibility 
in the language -- it'd have to be punted to Py3k.



More information about the Python-list mailing list