advanced module/import/namespace idioms

C

chuck

Every once in awhile I run across a python module that might have
statements like:

for c in sys.modules[module].__dict__.values():

or

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

Snurf also does some strange import trickory (see
http://bdash.net.nz/svn/snurf/trunk/snurf/dataStore/).

While I'm sure none of this stuff is rocket science, but rather just
"namespace manipulation" there is very little explanation in the python
world that explains these techniques. I've googled around to try to
find articles/papers that explain such techniques but cannot find any.
Can anyone point out any refereces that discuss such
module/import/namespace idioms.
 
S

Scott David Daniels

chuck said:
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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top