""" Выполняет обход дерева наследования снизу вверх, используя ссылки на пространства имен, и отображает суперклассы с отступами """ def classtree(cls, indent): print('.' * indent + cls.__name__) for supercls in cls.__bases__: classtree(supercls, indent+3) def instancetree(inst): print('Tree of', inst) classtree(inst.__class__, 3) def selftest(): class A: pass class B(A): pass class C(A): pass class D(B,C): pass class E: pass class F(D,E): pass instancetree(B()) instancetree(F()) if __name__ == '__main__': selftest() 

If you run this code, the tree traversal will be performed as it is. but if you add the return statement to the classtree [ return classtree (supercls, indent + 3)] function call, the tree is not completely traversed. Why is that? what is the difference.

  • @Maksim is like? To leave after processing the first value from cls.__bases__ ? Give the code - alexlz
  • The code cited above. It works as it should. Ie completely bypasses the class tree. I understand it this way: when you call instancetree (F ()), several recursive calls are executed, after receiving the last value, the call stack is emptied and the execution flow goes into the for loop for class C processing. traversal of a tree occurs only on one branch - Maksim
  • The skin also cited above. It works as it should. I understand it this way: when you call instancetree (F ()), several recursive calls are created. after receiving the last value, the call stack is emptied and control is transferred to the for loop to process class C.
  • @Maksim I do not see any return in the above code. And where you insert it, I can not understand. I expressed one hypothesis, but whether I understood correctly is unknown. If so: for supercls in cls .__ bases__: return classtree (supercls, indent + 3) then this is an obvious nonsense. And with telepathy, I strained today. And the rest of the days are no better. - alexlz
  • @Maksim, you are asked to bring in a non-working code. Probably comrade @alexlz (like me) suspects that you insert a return into the loop. Give the inoperative code - and you will see what you are doing wrong - BOPOH

0