473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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__.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.

Dec 31 '05 #1
1 1345
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
sc***********@acm.org
Dec 31 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Paul Rubin | last post by:
That's what the Python style guides advise. They don't seem to like def frob(x): import re if re.search('sdfxyz', x): ... instead preferring that you pollute your module's global namespace...
4
by: Brad Tilley | last post by:
When memory usage is a concern, is it better to do: from X import Y or import X Also, is there a way to load and unload modules as they are needed. I have some scripts that sleep for...
8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
8
by: glomde | last post by:
Hi, I want to create a function that preprocesses a file and then imports the parsed file. What I found out is that you can do something like this: def ImportFile(fileName): parsedCode =...
5
by: Claus Tondering | last post by:
I understand that you can use the imp module to programmatically mimic the "import xyzzy" statement. But is there any way to programmatically mimic the "from xyzzy import *" statment? --...
12
by: reubendb | last post by:
Hello, I am new to Python. I have the following question / problem. I have a visualization software with command-line interface (CLI), which essentially is a Python (v. 2.5) interpreter with...
2
by: gbin,Zhou | last post by:
Hi,all. I see from "http://docs.python.org/tut/node11.html" that "Name spaces are created at different moments and have different lifetimes. The namespace containing the built-in names is created...
4
by: bvdp | last post by:
Terry Reedy wrote: <snip> <snip> <snip>
0
by: Fredrik Lundh | last post by:
Jeff Dyke wrote: so how did that processing use the "mymodulename" name? the calling method has nothing to do with what's considered to be a local variable in the method being called, so...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.