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

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

Post by Dave Ange » Sat, 11 Apr 2009 13:22:09



Your sample is very hard to follow, because you're mis-using the word
built-in. Built-ins are part of the Python implementation, and
available everywhere, while you apparently just mean a function visible
in foo.py, which may have been imported from somewhere else.

One solution would be that after importing mymod, you assign to its
global space, as follows:

def some_special_function():
.....
import mymod
mymod.some_special_function = some_special_function


You're adding a global name to the mymod namespace, and giving it a
reference to a function in your own space.
Note that the two names don't have to be the same in the two files. You
could use, for example:
import os
mymod.some_special_function = os.path.join
 
 
 

1. Why 'import module' will not import module.py but the directory module?

2. Global variables in modules---preserving values between module calls

Hello all,

I sometimes make use of global variables within modules, which I denote
with "static" so that they will not be seen outside the module, and
will preserve values between module calls.

Recently I was looking back at a rather important function and found
I'd forgotten to put the static declaration in place. I'm not worried
about any interference with outside---the variable names are unlikely
to be reproduced elsewhere---but I am *very* concerned that the values
stored in these variables should have been preserved between module
calls. Else it means I have to throw a lot of data away... :-(

Is this latter feature standard for globals in modules, or is the
"static" designation required here? Certainly the results of the code
would seem to suggest the values were preserved. However, it's not
clear to me that this wasn't just the compiler (gcc -O3 -ansi -pedantic
-Wall ...) being nice to me.

Many thanks,

-- Joe

3. [patch 01/10] Immediate values - Global modules list and module mutex

4. [patch 1/8] Immediate Values - Global Modules List and Module Mutex

5. [patch 1/8] Immediate Values - Global Modules List and Module Mutex

6. [PATCH 1/4] Dynamic Debug: Introduce global fake module param module.ddebug - V4

7. [PATCH 1/4] Dynamic Debug: Introduce global fake module param module.ddebug - V4

8. Help with a standard module to get current module name

9. Obtaining System.Refelction.Module for the current module

10. Supply Mini-Com Coupler Modules ,Mini-com Coupler Modules,Panduit Mini-com Coupler Module,Panduit Minicom Modules,Panduit Mini-Com Module,Panduit Mini-com Fiber Optic Adapter Modules

11. Making module content available in parent module

12. Supply Mini-Com Coupler Modules ,Mini-com Coupler Modules,Panduit Mini-com Coupler Module,Panduit Minicom Modules,Panduit Mini-Com Module,Panduit Mini-com Fiber Optic Adapter Modules

13. Modules(index) and Modules("name") return different modules?