advanced module/import/namespace idioms

Scott David Daniels scott.daniels at acm.org
Sat Dec 31 14:25:17 EST 2005


chuck wrote:
> Every once in awhile I run across a python module that might have
> statements like:
> 
> for c in sys.modules[module].__dict__.values():

Straight-forwardly imported modules wind up in sys.modules,
keyed by their module name.  So  "t = sys.modules['name']"
is like "import name as t".

Work it out in an interactive session, and you can get to know
what is going on here.  If you discover something wonderful you
wish were written up, write it up.


> import __builtin__
> __builtin__.__dict__['_'] = lambda x: x

The __builtin__ module is magical -- the source of predefined names.
_Do_not_muck_about_with_it_in_delivered_code_ (I know it is tempting).
The reason to avoid playing with it is that you are slightly breaking
the Python environment all code runs in.

--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list