[Tutor] module imports

Bob Gailer bgailer at alum.rpi.edu
Thu Mar 9 01:09:37 CET 2006


kevin parks wrote:
> i have a module called foo.py
>
> foo.py imports random
>
> kp.py imports foo.py ... but in kp.py i also need to use stuff from 
> random...
>
> so kp.py also imports random....
>
> but i prolly shouldn't do that right?
>   
Wrong.
> Cause now, haven't i needlessly copied the same 
> names/attributes/methods/functions to 2 namespaces...
>   
The first import of a module runs its top level code, and creates a 
module object. Subsequent imports simply place a reference to that 
object in the current namespace.
> I get very confused about imports... and accessing names from other 
> spaces and such...
> and efficiency.
>
> i have 2 or more modules all of which need to access a given function 
> or method, what is the best way to do that.
>   
Put the function in a module and import it as needed.
> i am so cornfused
>   
Apparently. Even your spell checker is addled.


More information about the Tutor mailing list