1

Consider the following code:

class A:
    def foo(self, a):
        return a
    def bar(self, a):
        print(foo(a))

class B(A):
   def foo(self, a):
       return a[0]

Now calling B.bar(a) the result is print(a[0]), but what I want is print(a). More directly: I'd like that the calling of bar()in a child class uses the definition of foogiven in A even if overridden. How do i do that?

Jonah Bishop
  • 12,279
  • 6
  • 49
  • 74
L.A.
  • 243
  • 2
  • 12

1 Answers1

0

I believe this is what you are looking for:

class A(object):
    def foo(self, a):
        return a
    def bar(self, a):
        print(A.foo(self,a))

class B(A):
   def foo(self, a):
       return a[0]

or alternatively:

class A:
    def foo(self, a):
        return a
    def bar(self, a):
        print(self.foo(a))

class B(A):
   def foo(self, a):
       return super().foo(a)
gold_cy
  • 13,648
  • 3
  • 23
  • 45