How To Import Class From Class Included By That Class
Solution 1:
Indeed - if AAA.py
imports something from BBB.py
at top level and vice versa, it doesn't work as intended.
There are two ways you can solve it:
Import the modules from each other. This way they are both present as their namespace and will be filled during the import process.
So just do
import BBB
and useBBB.BBB()
for instantiating the class:import BBB classAAA(): deftest(self): print'AAAA' a = BBB.BBB()
Do the import where you need it:
classAAA(): deftest(self): from BBB import BBB print'AAAA' a = BBB()
This way the link between the two modules is "looser" and not so tight.
Solution 2:
When you want to use a module in another one, you have to import it and so use import your_module
. You have to type your_module.foo()
if you want to use a method inside it. With the instruction from your_module import attr1, foo1, [...]
you are modifying the module's global variables, so that you can use attr1 or the method foo1 as if they were in your module.
A concrete example is: if you want to use the math
module, you type import math
and when you want to use the constant pi you type math.pi
, but if you are sure there will be no clash with the other names, you'll type from math import pi
and you will use the constant pi
as if you declared it in your module.
Post a Comment for "How To Import Class From Class Included By That Class"