Feature request: String-inferred names
Terry Reedy
tjreedy at udel.edu
Mon Nov 30 16:35:12 EST 2009
Brad Harms wrote:
> Well, yes, the names would have to be determined at run time. That's
> what getattr and setattr do, except that that do it in the context of an
> object rather than the local scope. However, I was under the impression
> that python's mechanism for looking up local names was the same as the
> mechanism used to look up attributes because names and attributes seem
> to function pretty much the same way. This I assumed because of the
> functionality of the locals() and globals() functions, which seem to act
> like the __dict__ attribute on objects except that the work on the
> current scope.
The definition of locals() allows it to just be a dict *copy* of the
local namespace, rather than the local namespace itself. Within
functions, (at least for CPython, and probably for other
implementations), locals() is just a copy, and changes to locals() are
*not* propagated back to the local namespace. Within functions, local
name 'lookup' has nothing to do with dict lookup. (It is more like list
indexing.)
More information about the Python-list
mailing list