[Python-Dev] Early PEP draft (For Python 3000?)
eyal.lotem at gmail.com
Tue Oct 11 23:31:42 CEST 2005
I would like to re-suggest a suggestion I have made in the past, but
with a mild difference, and a narrower scope.
Name: Attribute access for all namespaces
Rationale: globals() access is conceptually the same as setting the
module's attributes but uses a different idiom (access of the dict
directly). Also, locals() returns a dict, which implies it can affect
the local scope, but quietly ignores changes. Using attribute access
to access the local/global namespaces just as that is used in instance
namespaces and other modules' namespaces, could reduce the mental
footprint of Python.
Method: All namespace accesses are attribute accesses, and not direct
__dict__ accesses. Thus globals() is replaced by a "module" keyword
(or "magic variable"?) that evaluates to the module object. Thus,
reading/writing globals in module X, uses getattr/setattr on the
module object, just like doing so in module Y would be. locals()
would return be replaced by a function that returns the frame object
(or a weaker equivalent of a frame object) of the currently running
function. This object will represent the local namespace and will
allow attribute getting/setting to read/write attributes. Or it can
disallow attribute setting.
global x ; x = 1
module.x = 1
globals()[x] = 1
setattr(module, x, 1)
locals()['x'] = 1 # Quietly fails!
frame.x = 1 # Raises error
x = locals()[varname]
x = getattr(frame, varname)
- Python becomes more consistent w.r.t namespacing and scopes.
- "module" is already possible by importing one's own module, but that is:
* Confusing and unnecessarily requires naming one's self
redundantly (Making renaming of the module a bit more difficult).
* Not easily possible in a __main__/importable module.
* No equivalent for locals()
- Automatic script conversion may be difficult in some use cases of
More information about the Python-Dev