33

Suppose I have two classes (one a parent and one a subclass). How do I refer to a method in the parent class if the method is also defined in the subclass different?

Here is the code:

class A:
    def __init__(self, num):
        self.value=num
    def f(self, num):
        return self.value+2
class B(A):
    def f(self, num):
        return 7*self.f(num)

In the very last line, I want to refer to the parent class A with the "self.f(num)" command, not the method itself in B which would create an infinite recursion. Thank you in advance.

user1111042
  • 1,461
  • 4
  • 18
  • 23

7 Answers7

42

If you know you want to use A you can also explicitly refer to A in this way:

class B(A):
    def f(self,num): 
        return 7 * A.f(self,num)

remember you have to explicitly give the self argument to the member function A.f()

jimifiki
  • 5,377
  • 2
  • 34
  • 60
  • I had an error with doing that, when I tried to call a method, which was implemented in C. It expected an instance of A as first parameter and didn't recognize B as a child class. I suppose that using super() might get around that, but I'm not sure yet. – erikbstack Jun 25 '13 at 09:16
41

Use super:

return 7 * super(B, self).f(num)

Or in python 3, it's just:

return 7 * super().f(num)
tzaman
  • 46,925
  • 11
  • 90
  • 115
  • this is theoretically right, but for some reason, I get: TypeError: super() argument 1 must be type, not classobj – user1111042 Feb 19 '12 at 07:14
  • 6
    @user1111042: Let me guess - you are using Python 2.x? Then your _class A_ should inherit from _object_, only then you get a new style class. New style classes are instances of class _type_, whereas old style classes are just instances of _classobj_. _super()_ only works on new style classes. – pillmuncher Feb 19 '12 at 07:23
  • Yes, I am using Python 2.x and super as you said does not work in 2.x – user1111042 Feb 19 '12 at 07:29
  • 3
    Oh, right. Old-style classes, I forgot. You should really be defining A as `class A(object)`, after which this will work. – tzaman Feb 19 '12 at 07:36
6

In line with the other answers, there are multiple ways to call super class methods (including the constructor), however in Python-3.x the process has been simplified:

Python-2.x

class A(object):
 def __init__(self):
   print "world"

class B(A):
 def __init__(self):
   print "hello"
   super(B, self).__init__()

Python-3.x

class A(object):
 def __init__(self):
   print "world"

class B(A):
 def __init__(self):
   print "hello"
   super().__init__()

super() is now equivalent to super(<containing classname>, self) as per the docs.

Aidan Gomez
  • 8,167
  • 5
  • 28
  • 51
2

Why not keep it simple?

class B(A):
    def f(self, num):
        return 7 * A.f(self, num)
pillmuncher
  • 10,094
  • 2
  • 35
  • 33
1
class B(A):
    def f(self, num):
        return 7 * super(B, self).f(num)
wong2
  • 34,358
  • 48
  • 134
  • 179
1

you can use super or if you can be more explicit and do something like this.

class B(A):
  def f(self, num):
    return 7 * A.f(self, num)
Doboy
  • 10,411
  • 11
  • 40
  • 48
0

Check out my answer at Call a parent class's method from child class in Python?.

It's a slight twist on some others here (that don't use super).

Community
  • 1
  • 1
BuvinJ
  • 10,221
  • 5
  • 83
  • 96