Is cicular inheritance allowed in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is cicular inheritance allowed in python?

class A: def method(self): print("A method") class B(A): def another_method(self): print("B method") class C(B): def third_method(self): print("C method") class A(C): def fourth_method(self): print("D method") c = C() c.method() c.another_method() c.third_method() A().fourth_method ()

14th Jun 2018, 11:30 AM
Keshav Gupta
Keshav Gupta - avatar
2 Answers
+ 2
Apparently it is, since if you replace the last 5 lines with the following: a = A() a.method() a.another_method() a.third_method() a.fourth_method() The code runs fine, so class A seems to have inherited the other three methods. Edit: I really should've googled this before posing my answer, but If 李立威's idea of circular inheritance is what it is, then I don't think it exists in Python.
14th Jun 2018, 12:54 PM
Just A Rather Ridiculously Long Username
0
To my understanding, no. In your code, you've redefined an A(C) class, that is, you've impliciy overwrite (or del) the original class A. However, since class B is inherited from the original class A, it's method "method" remains. (just like this logic: a=1 b=a+1 c=b+1 a=c+1 #this "a" variable is a new one, which is different from the original variable "a". The original variable "a" or the original class A has been overwritten. ) If the circular inheritance truely exists, then the following code should be valid: b=B() b.fourth_method() And that is not working. Edit: see page 3 at https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2469/
14th Jun 2018, 4:33 PM
李立威