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
  • 11,967
  • 5
  • 44
  • 72
L.A.
  • 208
  • 2
  • 11

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
  • 12,080
  • 3
  • 20
  • 42