This is the python code that causes me problems :
# -*- coding: utf-8 -*-
class ObjectType2 (object):
def __init__ (self):
self.name = ""
self.value2 = 0
class ObjectType1 (object):
def __init (self):
self.value = 0
self.variables = []
class MainStruct (object):
def __init__ (self):
index1 = 0
index2 = 0
self.objects1 = []
for index1 in [0, 1, 2]:
self.objects1.append(ObjectType1())
self.objects1[index1].value = index1
self.objects1[index1].variables.append(ObjectType2())
if __name__ == '__main__':
foobar = MainStruct()
for x in foobar.objects1:
print(x.value)
When I execute it, I've got the folowing error message :
Traceback (most recent call last):
File "C:\Users\Franck\Documents\Developpement\python\dbc_file_reader \test1.py", line 31, in <module>
foobar = MainStruct()
File "C:\Users\Franck\Documents\Developpement\python\dbc_file_reader\test1.py", line 25, in __init__
self.objects1[index1].variables.append(ObjectType2())
AttributeError: 'ObjectType1' object has no attribute 'variables'
It looks like it's related to the empty list initialisation in ObjectType1, but I can't figure out what the problem is. Could somebody point me out where is the issue ?