I tried to create a data structure in python, where I have an outerClass, innerClass, both would include several variables and the outerClass has a list, storing objects of innerClass instances.
I managed to create the outerClass instances but failed to do it with innerClass instances, especially unable to append them to my innerInstancesList.
I'm quite new to python, not sure exactly if this is the best way implement this structure. Trying to make something similar:
Outerinstance1
variable1
variable2
Innerinstance1
variable1
variable2
Innerinstance2
variable1
variable2
Innerinstance3
variable1
Outerinstance2
variable1
Is there a better way to implement this in python? My sample code:
class outerClass:
def __init__(self, name):
self.name = name
self.innerInstancesList= []
self.innerClass = self.innerClass()
class innerClass:
def __init__(self):
self.id = 0
self.c = "char"
def init2(self, id, c):
self.id = id
self.c = c
outerInstance = outerClass("Outerinstance1")
print (hex(id(outerInstance)))
for a in range(0,5):
outerInstance.innerClass.init2(1, a)
x = outerInstance.innerClass
print (hex(id(x)))
outerInstance.innerInstancesList.append(x)