advanced module/import/namespace idioms

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chuck

    #1

    advanced module/import/namespace idioms

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

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

    or

    import __builtin__
    __builtin__.__d ict__['_'] = 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.

  • Scott David Daniels

    #2
    Re: advanced module/import/namespace idioms

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

    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.

    [color=blue]
    > import __builtin__
    > __builtin__.__d ict__['_'] = lambda x: x[/color]

    The __builtin__ module is magical -- the source of predefined names.
    _Do_not_muck_ab out_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@a cm.org

    Comment

    Working...