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


Messages In This Thread
Empty attribute class dictionary after saving it in a class object dictionary - by 3dimensions - May-20-2018, 12:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using Lists as Dictionary Values bfallert 8 258 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  How does this code create a class? Pedroski55 6 331 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 251 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Printing out incidence values for Class Object SquderDragon 3 288 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  class and runtime akbarza 4 366 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 532 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 654 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 735 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Matching Data - Help - Dictionary manuel174102 1 403 Feb-02-2024, 04:47 PM
Last Post: deanhystad
  Class test : good way to split methods into several files paul18fr 4 485 Jan-30-2024, 11:46 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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