How to import a module so that the current globals are available to the module?

Carl Banks pavlovevidence at gmail.com
Thu Apr 9 18:57:05 EDT 2009


On Apr 9, 2:25 pm, mrstevegross <mrstevegr... at gmail.com> wrote:
> I'm trying to import a module so that the globals() of the importer
> module are available to the imported module itself. [snip]


In general, the way I recommend to deal with this issue (aside from
reorganizing your code) is to pass the function you want to call in as
an argument.  Rewrite your go function like so:


def go(function_to_call):
    # do stuff
    function_to_call(1,2)
    # do rest of stuff


Then, from the other modules, call it like this:

mymod.go(some_special_func)


What you are looking to do isn't THAT unreasonable, and there are some
libraries that are based strongly on callbacks (which is the term for
what you're trying to do).  However, few if any libraries try to
determine the callback automatically; you have to explicity pass the
function/object to call back to.


Carl Banks



More information about the Python-list mailing list