Let's say I have two Python modules:
module1.py :
import module2 def myFunct(): print "called from module1"
module2.py :
def myFunct(): print "called from module2" def someFunct(): print "also called from module2"
If I import module1 , is it better to etiquette re-import module2 or just reference it as module1.module2 ?
For example ( someotherfile.py ):
import module1 module1.myFunct() # prints "called from module1" module1.module2.myFunct() # prints "called from module2"
I can also do this: module2 = module1.module2 . Now I can directly call module2.myFunct() .
However, I can change module1.py to:
from module2 import * def myFunct(): print "called from module1"
Now, in someotherfile.py , I can do this:
import module1 module1.myFunct() # prints "called from module1"; overrides module2 module1.someFunct() # prints "also called from module2"
Also, by importing * , help ('module1') shows all the functions from module2 .
On the other hand, (if module1.py uses import module2 ), I can do: someotherfile.py :
import module1, module2 module1.myFunct() # prints "called from module1" module2.myFunct() # prints "called from module2"
Again, what is the best etiquette and practice? To import module2 again, or just reference module1 import?