Home > database >  How to add up all instances of a class. This total value will be available through the class as well
How to add up all instances of a class. This total value will be available through the class as well

Time:10-30

Any instance of the class Square starts as an integer equal to 2. When the square_me method is called on the instance, the instance is multiplied by itself. This can be done any number of times (e.g. calling square_me once on an instance results in the value 4, and calling square_me again on the same instance results in the value 16, etc.).

class Square:

    def __init__(self):
        self.integer = 2

    def square_me(self):
        self.integer = self.integer * self.integer

    def get_me(self):
        return self.integer

n1 = Square()
n2 = Square()
n3 = Square()

n1.square_me()
val1 = n1.get_me()
print(val1)               # 4

n2.square_me()
n2.square_me()
val2 = n2.get_me()
print(val2)               # 16

This was straightforward enough, however, I also want the class Square to keep track of the total value of all instances. This total value should be available through the class as well as any of the instances.

For example:

print(val1)               # 4
print(val2)               # 16
print(Square.total)       # 20

I tried adding each instance to a dictionary and summing the values within it. While I can sum the dictionary values outside of the class and get the correct answer with print(sum(Square.instances.values())), I want to design it so that the class Square tracks the total value of all instances. Instead, it just prints:

<function Square.get_total at 0x000001CADA21B0D0>

I feel like I'm close to a solution but need some guidance. This is my code:

class Square:

    instances = {}
    total = sum(instances.values())

    def __init__(self):
        self.integer = 2

    def square_me(self):
        self.integer = self.integer * self.integer
        self.instances[self] = (self.integer)

    def get_total(self):
        return self.total

    def get_me(self):
        return self.integer

n1 = Square()
n2 = Square()

n1.square_me()
val1 = n1.get_me()
print(val1)               # 4

n2.square_me()
n2.square_me()
val2 = n2.get_me()
print(val2)               # 16

print(Square.instances) # Print the dictionary.
print(sum(Square.instances.values())) # This works.
print(Square.get_total) # This does not work.

CodePudding user response:

Square.get_total is a function. You need Square.get_total(). In that case, get_total is not going to get a self argument. Also, you never update total, so it's going to return anything. You would need get_total to do return sum(Square.instances.values()). Also, you'll want to store the instance in the list during initialization, not just after a square_me call. Like this:

class Square:

    instances = {}

    def __init__(self):
        self.integer = 2
        Square.instances[self] = self.integer

    def square_me(self):
        self.integer = self.integer * self.integer
        Square.instances[self] = self.integer

    @classmethod
    def get_total(cls):
        return sum(cls.instances.values())

    def get_me(self):
        return self.integer

n1 = Square()
n2 = Square()

n1.square_me()
val1 = n1.get_me()
print(val1)               # 4

n2.square_me()
n2.square_me()
val2 = n2.get_me()
print(val2)               # 16
print(sum(Square.instances.values())) # This works.
print(Square.get_total()) # This does not work.

Output:

4
16
20
20
  • Related