Even searching through many similar questions I could not find how I could create recursive class tree structure (adding branch with leaves to the root) in python. Maybe someone could advice why this type of code does not work in python and how it should be written correctly?
import copy
class Tree:
children = []
def add_child(self, child):
self.children.append(copy.deepcopy(child))
level1 = Tree()
child_level2 = Tree()
child_level3 = Tree()
child_level2.add_child(child_level3)
print('After adding level 3 to level 2 children number is {}'.format(len(child_level2.children)), child_level2.children)
level1.add_child(child_level2)
print('After adding level 2 to level 1, level 1 children number is {} but expecting expecting to have 1 child'.format(len(level1.children)), level1.children)
print('After adding level 2 to level 1, level 2 children number is {} but expecting expecting to have 1 child'.format(len(level1.children[0].children)),level1.children[0].children)
p.s. Anytree library works for creating this structure but it is difficult to adapt that in the full project.