Circular import
Christian Tanzer
tanzer at swing.co.at
Mon Jun 19 01:47:50 EDT 2000
"Frank V. Castellucci" <frankc at colconsulting.com> wrote:
> 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
In your example, you don't actually reference Composite in Leaf.py --
therefore no import is necessary.
If you really need to reference Composite in Leaf.py, there are two
simple options :
- import inside the function(s) which reference Composite
- put the import statement at the end of Leaf.py (this only works if
all references to Composite are inside functions, or more
accurately, if Composite is not referenced bvy any code executed
before the import statement)
Should you want to cross-reference at the module level you are out of luck.
--
Christian Tanzer tanzer at swing.co.at
Glasauergasse 32 Tel: +43 1 876 62 36
A-1130 Vienna, Austria Fax: +43 1 877 66 92
More information about the Python-list
mailing list