[Python] Python inheritance - how to call grandparent method?

Discussão em 'Python' iniciado por Stack, Julho 22, 2025.

  1. Stack

    Stack Membro Participativo

    Consider the following piece of code:

    class A:
    def foo(self):
    return "A"

    class B(A):
    def foo(self):
    return "B"

    class C(B):
    def foo(self):
    tmp = ... # call A's foo and store the result to tmp
    return "C"+tmp


    What shall be written instead of ... so that the grandparent method foo in class A is called? I tried super().foo(), but it just calls parent method foo in class B.

    I am using Python 3.

    Continue reading...

Compartilhe esta Página