10

Possible Duplicate:
Circular (or cyclic) imports in Python

I have class B that imports and creates instances of class A. Class A needs reference to B in its contructor and so includes B.

from a import A class B: def __init__(self): self.a = A() 

from b import B class A: def __init__(self, ref): assert isinstance(ref, B) self.ref = ref 

This doesn't work. The main file imports B and uses it... not. Something with the imports is wrong.

Error from file a ImportError: cannot import name B

3
  • 1
    No, that doesn't work. So, don't do that. Commented Apr 5, 2012 at 10:40
  • Not an answer but it is probably best to have a better design which doesn't require this circular import. Commented Apr 5, 2012 at 10:40
  • Understood. But this seems like a basic problem. Class needs reference to upper level... Commented Apr 5, 2012 at 10:41

3 Answers 3

21

Apart from "don't do that, you are painting yourself into a corner", you could also postpone the import of B until you need it. File a.py:

class A: def __init__(self, ref): from b import B assert isinstance(ref, B) self.ref = ref 

Class B won't be imported until you instantiate class A, by which time the module already has been imported fully by module b.

You can also use a common base class and test for that.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I will go with that. I somewhere saw this idea but couldn't understand. Your additional explanation is excellent!
3

Just import classes in __init__ method

class A: def __init__(self, ref): from b import B assert isinstance(ref, B) self.ref = ref 

Comments

1

The __init__ method executes when you create an instance of the class. In this case, you should get it to work by simply changing the import statements like this:

import a class B: def __init__(self): self.a = a.A(self) 

import b class A: def __init__(self, ref): assert isinstance(ref, b.B) self.ref = ref 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.