Help with static class attribute variables

I have a problem with ex45 of LPTHW. I’m trying to add a static variable to the Scene class. I want it to act as a score counter that maintains its value between scenes. This is so the Engine doesn’t get tangled up in score-keeping. So far, the children of Scene all end up creating their own local instances of the score.


class Parent(object):
        counter = 0

    # def incr_counter(self):
    #     counter += 1
        

class Child(Parent):
    pass


son = Child()
daughter = Child()
print("daughter.counter = ", daughter.counter)
print("son.counter = ", son.counter)
print("son.counter += 1")
son.counter += 1
print("daughter.counter = ", daughter.counter)
print("son.counter = ", son.counter)

I’m reading very long-winded explanations of why this is difficult to do on stack overflow. Is there a simple explanation? What is a more straightforward alternative that won’t cause inconsistencies later on? Is it better/more practical to use a global variable? Is it wiser to store persistent (static) variables in the Engine? I’ve posted these questions to the discord server too. look under level1.

Hey Mate,

I don’t even know if I can explain it either.

You could try putting counter like this.

class Parent(object):
    counter = 1

class Child(Parent):
    def increase(self):
        self.storage = Parent.counter + 1
        return self.storage

p = Parent()
print p.counter
c = Child()
print c.increase()

best solution I can come up with and have no way to explain it except that parent holds a sort of “global” variable called counter which you can access via Parent.counter in Child class :space_invader:

Hey that’s the same thing Zed showed me in the discord. Yeah, it’s like the parent class is a container in global space. Thanks for your example. The way I was hoping to implement this was by having all instances of the child classes refer to the same counter attribute so that if the counter increased for one instance, it increased for all the others. I managed to do that by modifying the method you show here.

1 Like

Cool man, would love to see. Always good to learn.

# This is a demonstration of a class attribute implementation in Python 3
class Parent(object):
    x = 0

mother = Parent()
father = Parent()

print("""
class Parent(object):
    x = 0

mother = Pather()
father = Pather()""")

print("\nParent.x: ", Parent.x)
print("mother.x:", mother.x)
print("father.x", father.x)

Parent.x += 1
print("\nParent.x += 1")
print("\nParent.x: ", Parent.x)
print("mother.x:", mother.x)
print("father.x", father.x)

mother.x += 1
print("\nmother.x += 1")
print("Parent.x: ", Parent.x)
print("mother.x: ", mother.x)
print("father.x: " , father.x)
``

Yes, that’s actually one of the few uses of class variables. Another if is you want to have a cache of values, like with a network connection or similar. Then, every instance of a class is actually just using one pulled from a class variable that has a list of existing network connections.