Reputation: 37
I am having 2 Python files in same directory. one.py and two.py containing classes First and Second respectively. I want import classes and inherit each other and use methods defined in each other.
one.py
from two import Second
class First(Second):
def first(self):
print "first"
two.py
from one import First
class Second(First):
def second(self):
print "second"
while compiling I am getting following error. Is there any way I can overcome this. Please suggest alternative methods also.
Traceback (most recent call last):
File "C:\Users\uvijayac\Desktop\New folder\two.py", line 1, in <module>
from one import First
File "C:\Users\uvijayac\Desktop\New folder\one.py", line 1, in <module>
from two import Second
File "C:\Users\uvijayac\Desktop\New folder\two.py", line 1, in <module>
from one import First
ImportError: cannot import name First
Upvotes: 0
Views: 1226
Reputation: 4726
The actual problem you're encountering is that you're trying to do a circular import, which has nothing to do with your circular inheritance. (There's plenty of material on SO on how to avoid that.)
However, note that circular inheritance is also not possible, as a class is only available for subclassing once it's defined, and its definition includes being subclassed from the other class, which therefore also needs to already be defined, which requires... you get the point - you can't have circular inheritance.
Upvotes: 1