how to refer to a parent method in python?

0 votes
I have two classes. How can I refer to a method in the parent class if the method is also defined in the subclass different?

The code is as follows:

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)
 

Also, 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. How can I do this?
Apr 22 in Python by ana1504.k
• 7,870 points
42 views

1 answer to this question.

0 votes
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)

 You need to explicitly give the self argument to the member function A.f()
answered Apr 22 by SDeb
• 13,160 points

Related Questions In Python

0 votes
1 answer

How to add a new line in Python?

You can use '\n' for a next ...READ MORE

answered May 2, 2018 in Python by aayushi
• 750 points
72 views
0 votes
2 answers

How to calculate square root of a number in python?

calculate square root in python >>> import math ...READ MORE

answered Apr 2 in Python by anonymous
164 views
0 votes
1 answer

How to print first character of each word in upper case of a string in Python

You can use split() using space as ...READ MORE

answered Jun 1, 2018 in Python by george
• 200 points
1,565 views
0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

answered Jun 4, 2018 in Python by ariaholic
• 7,340 points
70 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 172 views
0 votes
1 answer

How to Reverse a list in Python

You can use the reversed function in ...READ MORE

answered Sep 28, 2018 in Python by SDeb
• 13,160 points
64 views
+1 vote
1 answer

How to Profile a script in Python

Python includes a profiler called cProfile. It ...READ MORE

answered Nov 21, 2018 in Python by SDeb
• 13,160 points
55 views