Import functions in current namespace

Markus Schreyer markus.schreyer at
Thu Jan 15 23:35:11 CET 2009

we embedded python into our application via Swig. Now we like to wrap the
raw API functionality into a nicer more handleable module, but instead of
repeating every function in this wrapper i thought about importing them into
the namespace of the wrapper module. I like to give you an example of what
we like to achive.

Swig generates a file called "" which contains the API classes and
Lets say the contains a function called foo(). The should
now import the function foo() of the module ppms into the local namespace.

the swig module:

def foo():

def foo0():

One possibility would be to repeat the function in the like:

def foo():
def foo0():
    return ppms.foo0()+1

Imho this is a little ugly. So I thought about importing the content of (the autgenerated one) into the namespace of the wrapper. Every
definition of a fuction in the wrapper should overwrite the function in the
imported module but a call to a function which is not defined in the wrapper
goes right through.

# - magic version

def foobar():
    return foo()  #calls foo of

def foo0():
    return foo0()+1 #calls foo0 of

Now using the magic version:

import wrapper # looks in wrapper for foo() since it's not defined it falls
back to
wrapper.foobar() # calls wrapper.foobar() calls
wrapper.foo0() # calls wrapper.foo0() calls ppms.foo0()

Hopefully the idea is not to stupid.. ;)
Tnx for your help.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list