I have a problem where I want to change the class of some child elements in a class recursively.
I have a working solution, which is the following:
class PerformanceAggregator:
def __init__(self, collection: pf.GenericPortfolioCollection):
self.collection = collection
self.change_children()
def __getattr__(self, item):
return getattr(self.collection, item, None)
def change_children(self):
for i in range(len(self.children)):
if isinstance(self.children[i], pf.GenericPortfolioCollection):
self.children[i] = PerformanceAggregator(self.children[i])
However, the change_children method is not very pythonic. It would be better with
class PerformanceAggregator2:
def __init__(self, collection: pf.GenericPortfolioCollection):
self.collection = collection
self.change_children()
def __getattr__(self, item):
return getattr(self.collection, item, None)
def change_children(self):
for child in self.children:
if isinstance(child, pf.GenericPortfolioCollection):
child = PerformanceAggregator(child)
But this method does not work as intended. It does not replace the "child" element as the first method does. Do anybody have an idea about what is wrong?
childis a variable that assigned to one ofself.children. When you reassign it usingchild = PerformanceAggregator(child), you are not changing the reference inself.childrenat all. You are just assigning the variablechildto the new instance, but nothing else happens with it since you never update the parent object.enumerate()so you get both the child and it's index.