Empty attribute class dictionary after saving it in a class object dictionary - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Empty attribute class dictionary after saving it in a class object dictionary (/thread-10426.html) |
Empty attribute class dictionary after saving it in a class object dictionary - 3dimensions - May-20-2018 I will try to explain what problem I have: I have an attribute class dictionary called "diccpuntscontrol" which is annoying me because for some unknown reason it's not working properly. Basically there is a problem when recovering the data of the whole class which previously has been saved on the dictionary "Dicc_Curses". class Cursa: Dicc_Curses = {} def __init__ (self, nomcursa, nummaxparticipants, numpuntscontrol, estatcursa = "creada", diccpuntscontrol = None, diccparticipants = None): self.nomcursa = nomcursa self.nummaxparticipants = nummaxparticipants self.numpuntscontrol = numpuntscontrol self.estatcursa = estatcursa if diccpuntscontrol is None: self.diccpuntscontrol = {} else: self.diccpuntscontrol = diccpuntscontrol if diccparticipants is None: self.diccparticipants = {} else: self.diccparticipants = diccparticipants def guardar_cursa (nomcursa, cursa): Cursa.Dicc_Curses[nomcursa] = cursa def get_cursa (nomcursa): return Cursa.Dicc_Curses[nomcursa] def print (self): print (self.diccpuntscontrol) # How I save the race, of course this belongs to another part of the code, just copying here if it helps c = Cursa (nomcursa, nummaxcorredors, 2, "creada", ppc_dicc) # ppc_dicc is the dictionary that should become diccpuntscontrol and it is NOT empty Cursa.guardar_cursa (nomcursa, c) # How do I restore the data of the race, of course this belongs to another part of the code, just copying here if it helps c = Cursa.get_cursa (nomcursa) c.print() # c.print is here for testing purposes only The saving function "guardar_cursa" saves the object "cursa" and the name of the race "nomcursa" in "Dicc_Curses".The recovering function "get_cursa" searches Dicc_Curses and let's you get the data of the race. All of the class variables except "diccpuntscontrol" show properly but for some unknown reason "diccpuntscontrol" is always {} when it shouldn't be. If it helps also, I made a testing code to see whether the problems gets fixed somehow and surprisingly here works properly: class Cursa: Dicc_Curses = {} def __init__ (self, nomcursa, diccpuntscontrol = None): self.nomcursa = nomcursa if diccpuntscontrol is None: self.diccpuntscontrol = {} else: self.diccpuntscontrol = diccpuntscontrol def guardar_cursa (nomcursa, cursa): Cursa.Dicc_Curses[nomcursa] = cursa def get_cursa (nomcursa): return Cursa.Dicc_Curses[nomcursa] def print (self): print (self.diccpuntscontrol) c = Cursa ("nomcursa", {'S': {}, 'A': {}}) c.print() Cursa.guardar_cursa ("nomcursa", c) d = Cursa.get_cursa ("nomcursa") d.print()
RE: Empty attribute class dictionary after saving it in a class object dictionary - volcano63 - May-20-2018 It is working - at a cursor glance - properly; you defined get_cursar and guardar_cursar wrong, they should have been defined as staticmethod . Though classmethod would have served you better@classmethod def guardar_cursa (cls, nomcursa, cursa): cls.Dicc_Curses[nomcursa] = cursa @classmethod def get_cursa (cls, nomcursa): return cls.Dicc_Curses[nomcursa] In Python, the first argument of a method is always a reference to an object; self is just a convention, so - without a proper decorator - nomcursa is interpreted as self substitutionPS Please, don't use long concatenated words as variable name; they are unreadable, use underscores PPS When I said "properly" - I meant that it works exactly as you defined it; not the way you intended it to work. RE: Empty attribute class dictionary after saving it in a class object dictionary - 3dimensions - May-20-2018 If I do what you said the compiler complains, that's why I avoided using self in this functions since I do not really needed because I can call the dictionary saving objects without self. Also, if supposedly my code works properly why all the class variables are printed correctly except "diccpuntscontrol" and why in one program it doesn't work and in the testing program it works.
RE: Empty attribute class dictionary after saving it in a class object dictionary - volcano63 - May-20-2018 OK. spoon-feeding time - that would have worked in your previous code @staticmethod def guardar_cursa (nomcursa, cursa): Cursa.Dicc_Curses[nomcursa] = cursa @staticmethod def get_cursa (nomcursa): return Cursa.Dicc_Curses[nomcursa]You have to learn about classmethod and staticmethod - and how they differ from regular methods.
RE: Empty attribute class dictionary after saving it in a class object dictionary - 3dimensions - May-20-2018 My bad, I didn't know that I had to copy the @. I thought it was you telling me this was in x method. Anyway, this doesn't fix the issue unluckily. None of both methods. RE: Empty attribute class dictionary after saving it in a class object dictionary - volcano63 - May-20-2018 (May-20-2018, 01:23 PM)3dimensions Wrote: My bad, I didn't know that I had to copy the @. I thought it was you telling me this was in x method. Anyway, this doesn't fix the issue unluckily. Stupid me - thinking that your code makes sense. There's no connection between Cursa.Dicc_Curses and self.diccpuntscontrol . Forget everything that I said
RE: Empty attribute class dictionary after saving it in a class object dictionary - 3dimensions - May-20-2018 Okay, I found the issue but it makes no sense. For some unknown reason, "diccpuntscontrol" is not saving properly because the dictionary is returned from a class function. Making copies of said dictionary won't work either. This class declaration will print that diccpuntscontrol = {} ppc_dicc = # Call functions that returns a dictionary print (ppc_dicc) # To check that the dictionary is not empty and perfectly ok c = Cursa (nomcursa, nummaxcorredors, 2, "creada", ppc_dicc)Meanwhile this one will print properly: c = Cursa (nomcursa, nummaxcorredors, 2, "creada", {"Test": "Hello"})And this one too: ppc_dicc = {"Test": "Hello"} c = Cursa (nomcursa, nummaxcorredors, 2, "creada", ppc_dicc)How does this make sense????? The class itself: class PPC: Dicc_PPC = {} def guardar_ppc (self, nomppc): PPC.Dicc_PPC[nomppc] = {} def reset_dicc (self): PPC.Dicc_PPC.clear() def get_dicc (self): return PPC.Dicc_PPC Found a temporary solution which will let me continue working on the program but if someone has a proper answer to fix this issue post it please |