[Tutor] module imports

kevin parks kp8 at mac.com
Thu Mar 9 01:18:22 CET 2006


On Mar 8, 2006, at 7:09 PM, Bob Gailer wrote:

> 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.

so it is okay to do that?

>> 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.

but i mean a function from a standard module.

>> i am so cornfused
>>
> Apparently. Even your spell checker is addled.
[that was a joke]


so let's say i have a script called foo.py.
foo.py uses some things from the random module and therefore has

import random
import kp
import sys
import time

etc. & co.  in it

but foo.py also imports a module kp which also
happens to import random.

Should i only be importing random once or are you saying it
is just fine to import a module that imports another module that
you actually have already imported.


-kp--







More information about the Tutor mailing list