'How to Inherit Attributes in Python

Why does it print 0 and not 24? Also, why does it bring up an error if I dont explicitly define num in the System class even though im doing it in the constructor?

from abc import ABC, abstractmethod
class System(ABC):

    num = 0

    def __init__(self, input):
        self.num = input
        return

    @abstractmethod
    def getNum(self):
        pass

class FirstSystem(System):
    def __init__(self, input):
        super().__init__(input)
        return

    def getNum(self):
        return super().num

foo = FirstSystem(24)
print(foo.getNum())


Solution 1:[1]

super() explicitly calls the parent class and is used to access methods and objects that have been overwritten; ie, the exact opposite of what you want! As Capt. Trojan noted, self.num will get you the subclass version of num as you expect.

Here is the classic explaination of when and when not to use super.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eschibli