class A:
def add_f(self,a,b):
return a+b
class B:
def sum_f(self,a,b,c):
return A.add_f(a,b) + c
B1= B()
print B1.sum_f(1,2,3)
Traceback (most recent call last):
File "C:/Users/wxu/Documents/test.py", line 10, in <module>
print B1.sum_f(1,2,3)
File "C:/Users/wxu/Documents/test.py", line 7, in sum_f
return A.add_f(a,b) + c
TypeError: unbound method add_f() must be called with A instance as first argument (got int instance instead)
When I don't have the self for sum_f(self,a,b,c), it gave me this error:
Traceback (most recent call last):
File "C:/Users/wxu/Documents/test.py", line 10, in <module>
print test1.sum_f(1,2,3)
TypeError: sum_f() takes exactly 3 arguments (4 given)
Why is that? And how to call function add_f from class A in class B? Thank you for your help.
A.add_fadd_fan instance method if you need to call it without an instance ofA? Why make it part of a class at all? With the very little information we have, it seems like a better candidate for a module method.