Home > Blockchain >  Class X not have attributes Y
Class X not have attributes Y

Time:12-01

I don't understand this strange things...I've this class:

class cChallenge:
    def __init__(self, Id:int = 0 , Difficulty:str = '', Title:str = '',
                 Challenge:str = '', Url:str = '', Solved:bool = False):
        self.Id = Id
        self.Difficulty = Difficulty
        self.Title = Title
        self.Challenge = Challenge
        self.Url = Url
        self.Solved = Solved

Everything works fine so I've pushed all my generated cChallenge elements into a list: cList. If I query the list for single object everything works fine:

print(cList[1].Id)
>> 380

but if I use list comp:

print([x.Id for x in cList])
>> AttributeError: type object 'cChallenge' has no attribute 'Id'

[https://pastebin.com/cGuhPAG8] That's the link if someone want to try

CodePudding user response:

In the code of the complete script to fill the list you need to create an instance of a class i.e. each object (look at added variable c):

for s in submission:
#other code...
                c = cChallenge(Id, Difficulty, '', Url, Solved)
                cList.append(c)
#other code...

Don't know exactly what are you trying to put into the list, but probably this will do.

CodePudding user response:

Just like @Alecx note

to fill the list you need to create an instance of a class

so the right code for who look inside the pastebin is:

for s in submission
   ...
   _ = cChallenge(...)
   cList.append(_)

I will post the github link to the project tomorrow for who is interested!

  • Related