modules

Steven D. Majewski (sdm7g@elvis.med.virginia.edu)
Tue, 22 Mar 1994 20:15:31 -0500

Note that this same mechanism ( newmodule( name ) ) can be used to
(sort of) kludge the functionality of creating an internal module
within another module, but some additional work is required.

Variables can be created directly into the new namespace, but
functions and classes must be created in the outer space and
moved into the inner space ( copied and then deleted ) . The
global namespace is the namespace of the outer module.

[ It might be possible to create some helper functions that do
this automatically ]

If you make a module name that can't be imported directly, and use
( previously posted ) module() to access it by literal name, you
can make the scope even more "hidden".

It's far to awkward to use simply to simplify the public namespace
( at least without those helper functions ) - You will have to wait
for 'access' to be stable if you want real private functions, but
just to demonstrate that you CAN define symbols that can't be
directly imported or seen in dir(module) without any modifications
to the Python :

( names.module(name) accesses module via sys.modules[name] -
and was required to access a module by it's __name__ string. )

from newmod import newmodule
from names import module

newmodule( '# '+__name__+'.__invis' )
module( '# '+__name__+'.__invis' ).plus = lambda x,y: x + y

def add( a,b ):
return a + b

module( '# '+__name__+'.__invis' ).add = add
del add

def test( a,b ):
__invis = module( '# '+__name__+'.__invis' )
if __invis.add( a, b ) == __invis.plus( b, a ):
print OK

- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU>
- UVA Department of Molecular Physiology and Biological Physics