Circular Inheritance

Ian Bicking ianb at colorstudy.com
Wed Jul 2 01:32:08 CEST 2003


On Tue, 2003-07-01 at 17:36, jinal jhaveri wrote:
> B requires C
> C requires A
> A requires B
> 
> and when I try to do this using 
> 
> from ...import....
> it tells me that you cannot import this. 

Instead of using from...import..., just use import.  To explain:

A.py:
from B import B
class A:
    ... uses B

B.py:
from A import A
class B:
    ... uses A

Now, let's say you import A.  When you do that, the first line is
executed ("from B import B").  So B is imported, and its first line is
executed ("from A import A").  But the class A does not yet exist --
we're still just on the first line of that module.  The A module *does*
exist, and you can import it, it just hasn't been completely set up. 
This is why it will usually work to use a plain import, and just refer
to A.A in B (and B.B in A).

  Ian







More information about the Python-list mailing list