[Python-ideas] Module local namespaces
Josiah Carlson
jcarlson at uci.edu
Thu Jan 4 02:17:16 CET 2007
Matt Draisey <matt at draisey.ca> wrote:
[snip]
-1 . The only thing that possibly should be fixed is that modules
should only be inserted into sys.modules after they have been imported
completely and correctly.
> This would be a big change in module import semantics, but should have
> remarkably few consequences, as it really is an enforcement mechanism
> for good style.
How is it an enforcement for good style? If I can do exactly what I'm
doing now, then it isn't an enforcement mechanism in any way.
Regardless, the way to handle not allowing the rest of your program to
muck with partially initialized modules is to not use the standard
import machinery. There are various ways of emulating module loading,
many of which can allow you to insert the module into sys.modules
*after* the module was loaded completely correctly. I actually use one
of these methods to handle the dynamic loading and reloading of Python
macros in a source code editor, and none of the macros are available in
sys.modules .
> The copying from the temporary namespace into the
> module object would be a good place to insert a hook function to filter
> what objects are actually published to the module. You could by default
> not copy any object indentified by a leading underscore.
You can already do this with the following code:
__gl = globals()
for name in __gl.keys():
if name[:1] == '_' and len(name) > 1 and name.count('_') == 1:
del __gl[name]
del __gl
- Josiah
More information about the Python-ideas
mailing list