Home > Software design >  how we can use inheritance to access a parameter that is passed to a function with the same name but
how we can use inheritance to access a parameter that is passed to a function with the same name but

Time:08-02

I am new to inheritance and am trying to understand how we can use it to access a parameter that is passed to a function with the same name but in different classes.

For example:

class Subparser:
    def __init__(self):
        pass

    def test1(self, likes):
        self.likes = 72


class Parser(Subparser):
    def __init__(self):
        super().__init__()

    def test88(self):
        <how_can_I_access_self.likes_from_test1?>


B = Parser()
print(B.test1())

In the last line above, I am prompted to add one more argument into the B.test1() function, which makes sense as I need to fill in likes but my understanding is that since we have test1 function in both Classes with the same function name - how can I use inheritance to access the likes parameter in Subparser.test1 from Parser.test1?

i.e. my understanding is that when I run the above code, I should get 72 printed twice!!!

CodePudding user response:

Maybe you are looking for something like this, who knows?

class Subparser:

    def test1(self, like):
        self.like = like


class Parser(Subparser):

    def test88(self):
        return self.like


my_object = Parser()
my_object.test1(72)

print(my_object.test88())

If that's not what you want... well, maybe you need to read up on how classes work, and you'll be able to answer your own questions better.

  • Related