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, 2019 in Python by ana1504.k
• 7,910 points
996 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, 2019 by SDeb
• 13,300 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
1,221 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, 2019 in Python by anonymous
5,784 views
+1 vote
2 answers

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

class Solution:     def firstAlphabet(self, s):             self.s=s              k=''              k=k+s[0]              for i in range(len(s)):                     if ...READ MORE

answered Oct 28, 2020 in Python by Anurag
12,296 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 aryya
• 7,460 points
1,330 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,363 views
0 votes
1 answer
0 votes
1 answer

How to Reverse a list in Python

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

answered Sep 29, 2018 in Python by SDeb
• 13,300 points
794 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,300 points
1,770 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP