Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Try changing

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

to

def getNum(self):
    return self.num

and see if that helps 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading