Function to import module to namespace

B

bvdp

Is it possible to do this from a function: import a module and append
the defs in that module to an existing module/namesapce.

So, in my code I have something like:

# main code
import mods

def loadmore(n):
import_module(n, mods)

.....
# end of main

this will permit the addition of the the stuff in file 'n.py' to 'mods'.

Assuming that foo1() is defined in newmod, I should now be able to do
something like mods.foo1().

Thanks.
 
C

Cédric Lucantis

Le Sunday 29 June 2008 21:08:36 bvdp, vous avez écrit :
Is it possible to do this from a function: import a module and append
the defs in that module to an existing module/namesapce.

So, in my code I have something like:

# main code
import mods

def loadmore(n):
import_module(n, mods)

....
# end of main

this will permit the addition of the the stuff in file 'n.py' to 'mods'.

Assuming that foo1() is defined in newmod, I should now be able to do
something like mods.foo1().

You can dynamically add objects to a module:
'bar2'

and for the loading part you can use the __import__ builtin or maybe execfile
(see the 'built-in functions' chapter of the library reference for more about
these).
 
T

Terry Reedy

bvdp said:
Is it possible to do this from a function: import a module and append
the defs in that module to an existing module/namesapce.

So, in my code I have something like:

# main code
import mods

def loadmore(n):
import_module(n, mods)

....
# end of main

this will permit the addition of the the stuff in file 'n.py' to 'mods'.

Assuming that foo1() is defined in newmod, I should now be able to do
something like mods.foo1().

Do you mean something like this?['Formatter', 'Template', '_TemplateMetaclass', '__builtins__',
'__doc__', '__file__', '__name__', '__package__', '_multimap', '_re',
'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords',
'digits', 'hexdigits', 'maketrans', 'octdigits', 'printable',
'punctuation', 'whitespace']['Formatter', 'Template', '_TemplateMetaclass', '__builtins__',
'__doc__', '__file__', '__name__', '__package__', '_multimap', '_re',
'acos', 'acosh', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase',
'asin', 'asinh', 'atan', 'atan2', 'atanh', 'capwords', 'ceil',
'copysign', 'cos', 'cosh', 'degrees', 'digits', 'e', 'exp', 'fabs',
'factorial', 'floor', 'fmod', 'frexp', 'hexdigits', 'hypot', 'isinf',
'isnan', 'ldexp', 'log', 'log10', 'log1p', 'maketrans', 'modf',
'octdigits', 'pi', 'pow', 'printable', 'punctuation', 'radians', 'sin',
'sinh', 'sqrt', 'sum', 'tan', 'tanh', 'trunc', 'whitespace']

tjr
 
B

bvdp

Terry said:
Is it possible to do this from a function: import a module and append
the defs in that module to an existing module/namesapce.

So, in my code I have something like:

# main code
import mods

def loadmore(n):
import_module(n, mods)

....
# end of main

this will permit the addition of the the stuff in file 'n.py' to 'mods'.

Assuming that foo1() is defined in newmod, I should now be able to do
something like mods.foo1().

Do you mean something like this?['Formatter', 'Template', '_TemplateMetaclass', '__builtins__',
'__doc__', '__file__', '__name__', '__package__', '_multimap', '_re',
'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords',
'digits', 'hexdigits', 'maketrans', 'octdigits', 'printable',
'punctuation', 'whitespace']['Formatter', 'Template', '_TemplateMetaclass', '__builtins__',
'__doc__', '__file__', '__name__', '__package__', '_multimap', '_re',
'acos', 'acosh', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase',
'asin', 'asinh', 'atan', 'atan2', 'atanh', 'capwords', 'ceil',
'copysign', 'cos', 'cosh', 'degrees', 'digits', 'e', 'exp', 'fabs',
'factorial', 'floor', 'fmod', 'frexp', 'hexdigits', 'hypot', 'isinf',
'isnan', 'ldexp', 'log', 'log10', 'log1p', 'maketrans', 'modf',
'octdigits', 'pi', 'pow', 'printable', 'punctuation', 'radians', 'sin',
'sinh', 'sqrt', 'sum', 'tan', 'tanh', 'trunc', 'whitespace']

tjr

Yes, I think that's what I might need. I'll give it a go in my code and
see if that does work.

Thanks.
 

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,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top