Circular Inheritance

John Roth johnroth at ameritech.net
Tue Jul 1 20:40:56 EDT 2003


"jinal jhaveri" <jhaveri at usc.edu> wrote in message
news:mailman.1057099023.14043.python-list at python.org...
> Hi All,
>
> I have one question regarding circular inheritance
>
> I have 3 files
>
> 1) A.py , having module A and some other modules
> 2) B.py having module B and some other modules
> 3) C.py having module C and some other modules
>
> Now I want to import Module C in B.py
>
> but C requires A.py
> and A requires B.py
>
> so
>
> 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.
>
> So any suggestions on this?

You've run into one one of those things that is only
understandable if you remember that Python *executes*
the module during the import process.

When you say "import" something, Python suspends
importing the first module, and begins importing the
second.

Then if the second one tries to import the first, it sees
that it's in the module table, and tries to find what you
requested; but it's incomplete since it's stalled at the
first "import" statement so it probably won't find
whatever it was looking for.

The best way around this is to eliminate the circular
dependency. The result is clearer and easier to
maintain.

Otherwise, you have to be very specific about what
goes where in each module so that any resources that
one wants have actually been loaded when the other
executes. In particular, "from foo import *" simply
doesn't work with a circular import. If it's not in
the incomplete module, it won't get imported.

John Roth


>
> thank you
> Jinal
>
>
>






More information about the Python-list mailing list