On 5/9/2011 10:29 AM, Steven D'Aprano wrote:
If people then ask, how does the interpreter know the names?, I can add more detail: names are actually strings in a namespace, which is usually nothing more than a dict. Oh, and inside functions, it's a bit more complicated still. And so on.
Which is why I think it best to stick with 'A namespace is a many-to-one mapping (in other words, a function) of names to objects'. Any programmer should understand the abstractions 'mapping' and 'function'. Asking how the interpreter finds the object associated with a name amounts to asking how to do tabular lookup. Well, we basically know, though the details depends on the implementation of the table (mapping).
An interpreter can *implement* namespaces various ways. One is to objectify names and namespaces as strings and dicts. If the set of names in a namespace is fixed, another way is to objectify names and namespaces as ints and arrays. Python prohibits 'from x import *' within functions precisely to keep the set of local namespace names fixed. Therefore, CPython can and does always use C ints and array for function local namespaces.
-- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list