2
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.

5
  • You're not asking the right queston here - this code errors before it even tries to call A.add_f Commented Mar 12, 2015 at 1:11
  • Ok. So how do I get rid of the error first please? Thank you Commented Mar 12, 2015 at 1:19
  • See if this helps: stackoverflow.com/questions/14391828/… Commented Mar 12, 2015 at 1:28
  • Why make add_f an instance method if you need to call it without an instance of A? 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. Commented Mar 12, 2015 at 18:00
  • Hey @Catherine I updated my answer. You missed a small detail in it before. Commented Mar 12, 2015 at 18:30

2 Answers 2

7

There was a few things.

  1. When you define class methods, they must have self as the first parameter.
  2. The part where you had an error is where you tried to call B as a variable. B is a class, and you must call it like any other class. This also applies when you are calling A() in class B.

Revised code:

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

print B().sum_f(1, 2, 3)

Update: Thanks for taking my advice but you're still missing something. In class B you call a method from class A, but you need parentheses for that too! In class B, call class A as such:

A().add_f(a, b)

Sign up to request clarification or add additional context in comments.

4 Comments

labeling the first parameter self is convention, not a requirement.
She already creates the variable B1 with which she is using to access the object; she doesn't need to make another.
I see the asker edited the question using only part of my answer. I'll reflect that and update mine as well.
Thank you very much, ProfOak! It's working now. And you explained it very well!
0

Simple fixes, you just need to open and close the brackets on your class declarations, so class B becomes class B() (Same thing applied to class A). Also, you need to add some kind of variable such as self as the first argument of each method declared within a class. You never need to pass anything as this first argument to fill self, as python will do it for you.

As for your A.add_f issue, you just need to do as you did with B, and instantiate it an A object to something like A1, and then call the method of A1.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.