Circular import

Darrell Gallion darrell at dorb.com
Sun Jun 18 18:55:30 EDT 2000


From: "Frank V. Castellucci" <frankc at colconsulting.com>
>
> With the assumption that class A and class B reference each other, how
> do you avoid the Python error with:
>
> ---------- Composite.py -----------
>
> from Leaf import Leaf
>
> class Composite:
> def __init__( self ):
> self.__theLeaf = Leaf(self)
>
> -------- Leaf.py --------------
>
> from Composite import Composite
>
> class Leaf:
> def __init__( self, aComposite ):
> self.__theComposite = aComposit
>
>

I'm guessing you come from the land of C++, where the compiler wants to know
every detail.
Don't bother with this:
> from Composite import Composite

Leaf can accept any kind of object as the composite.
What ever methods the Composite has will be accessible.
It's a beautiful thing once you get the hang of it.

--Darrell Gallion







More information about the Python-list mailing list