[Tutor] Name tables questions

Bernard Lebel 3dbernard at gmail.com
Mon Dec 19 03:56:19 CET 2005


I have a few theoric questions regarding name tables. I wish to better
understand a few things about this aspect of Python, in particular how
module names and the import statements fit into the picture of name

- First of all, I understand each scope has its "local" name table,
containing all the names defined in this scope, no matter the origin
of the name. So, if you import a module in a scope, its name is added
to the local scope name table, correct?

- When you import a module, I understand the module name is appended
to Python dictionary of imported modules. I can see the name of the
module if I print out the sys.modules attribute. If the module name is
also added to the local scope name table, it does mean that the module
is added in two tables: the local scope name table and the imported
modules table. Is that correct?

- I *think* I have read in Learning Python (sorry I don't have the
book near me) that during execution, when Python doesn't find a name
in the current scope name table, then it will look up every
encompassing name table one after another, until it can find it, and
if it can't, will raise an error.
If I remember correctly about my reading, this lookup into an
encompassing name table has a performance cost. The author suggests
that, without regard to the practical considerations, it would not be
such a bad idea to pass down all possible names to functions and
encapsulated functions so name lookup into other tables could be
Now, let say I would do something like that, and be zealous at that,
to the point where I would write import statements for the same
modules in every possible function to define the module name there.
Since the module name is already in the module names dictionary but
not in the global name table, would there be any advantage, or
disadvantage in doing so?

Thanks in advance

More information about the Tutor mailing list