1

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 ?

1 Answer 1

4

The

def __init (self):

should be

def __init__(self):
          ↑↑

Without the two trailing underscores this is just a method like any other, not a constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

Oops ! Thanks for the answer. I spent literally hours searching for a solution, and it was a f****** typo !! Shame on me...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.