Python Forum
Empty attribute class dictionary after saving it in a class object dictionary
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Empty attribute class dictionary after saving it in a class object dictionary
#1
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
Output:
{}
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. Wall

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()
Output:
{'S': {}, 'A': {}} {'S': {}, 'A': {}}
Reply
#2
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 substitution

PS 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.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
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.
Error:
Cursa.guardar_cursa (nomcursa, c) TypeError: guardar_cursa() missing 1 required positional argument: 'cursa'
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.
Reply
#4
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.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
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.
Reply
#6
(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.

None of both methods.

Stupid me - thinking that your code makes sense. There's no connection between Cursa.Dicc_Curses and self.diccpuntscontrol. Forget everything that I said
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#7
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????? Wall


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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class and runtime akbarza 4 282 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 429 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 562 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 594 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Matching Data - Help - Dictionary manuel174102 1 356 Feb-02-2024, 04:47 PM
Last Post: deanhystad
  Class test : good way to split methods into several files paul18fr 4 403 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Good class design - with a Snake game as an example bear 1 1,713 Jan-24-2024, 08:36 AM
Last Post: annakenna
  question about __repr__ in a class akbarza 4 530 Jan-12-2024, 11:22 AM
Last Post: DeaD_EyE
  error in class: TypeError: 'str' object is not callable akbarza 2 445 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  Dictionary in a list bashage 2 495 Dec-27-2023, 04:04 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020