Home > front end >  Problem in class when use dictionary: AttributeError: 'My_Data' object has no attribute &#
Problem in class when use dictionary: AttributeError: 'My_Data' object has no attribute &#

Time:01-25

When i run this script, I get an error in the My_Dictionary[row[0]].Other.append(row[3]) row.

The problem certainly concerns the classes, because this code works correctly if: i use it without the Page1 class, but using only the My_Data class (without self in init). Thank you

The error is this:

AttributeError: 'My_Data' object has no attribute 'Other'

How can I fix? Thank you


class Page1(tk.Frame):
    def __init__(self, master, other, **kw):
        super().__init__(master, **kw)
        self.configure(bg='white')

        class My_Data():
            def __init__(self, Name, Year, Other):
                self.Name: str
                self.Year: float
                self.Other: float
                
        def function1(self):
            My_Dictionary = {}
            x = cursor.execute("sql")

            for row in x.fetchall():
                if row[0] not in My_Dictionary:
                    Data = My_Data(
                        Name=row[1],
                        Year=row[2],
                        Other=list())

                    My_Dictionary[row[0]] = info
                My_Dictionary[row[0]].Other.append(row[3])

CodePudding user response:

In 'My_Data', I don't see any variable definitions, but some weird syntax involving colons and data types. Consider the following code snippet:

self.Other = Other

CodePudding user response:

Your class My_Data should look like:

class My_Data():
     def __init__(self, Name: str, Year: float, Other: float):
        self.Name = Name
        self.Year = Year
        self.Other = Other
  • Related