From: brian z. <bz...@zi...> - 2001-04-27 13:28:51
|
At 08:34 AM 4/27/2001 -0400, Ron...@Ne... wrote: >I have modules A--> D. > >Module A imports modules B, C. >Module D imports module A, and it uses modules B and C. > >Module D has runtime errors because the interpreter says it doesn't >recognize B or C >inside module D - yet I imported module A, which imported B and C. > >So, it appears that all modules require explicit import statements. This is true. Unlike C/C++ header files, the module imports reside only in the module namespace in which they are imported. So for D to make use of B and C, you'll need to import both of them. Now it is possible from within D to use module B by calling D.A.B but this is not really recommended since you won't know if the module ever changes its import statements with any confidence other than an AttributeError. However it does show how you can get at imports bound in different namespaces just as you would an attribute of any other object. -- A.py -- import B, C def hi(): print "A says hi" -- B.py -- def hi(): print "B says hi" -- C.py -- def hi(): print "C says hi" -- D.py -- import A def hi(): print "D says hi" >>> import D >>> D.A.B.hi() B says hi >>> >Maybe this means I haven't used Java >enough? Quite possibly, stick to Python, it's better! ;) brian |