I have a structure representing some data in a following way:
class GenericDataHolder(object):
def __init__(self):
self.xxx=[]
self.yyy=[]
class A(object):
def __init__(self):
self.a1=GenericDataHolder()
self.a2=GenericDataHolder()
class B(object):
def __init__(self):
self.b1=GenericDataHolder()
self.b2=GenericDataHolder()
self.b3=A()
I would like to iterate over class B elements so that i will be able to access all nested objects like below:
b=B()
for i in b:
print i.xxx
print i.yyy
I have read that there are a few possible solutions to solve this:
- using metaclass
- implementing iterator protocol
- using itertools
This is all fine on basic examples, but I couldn't find any solution to the problem I am facing directly - having class which's elements are composed of other classes.