-1

This is my code. I am a beginner in Python. I try to call func in func2 and func3of class Apple. After I Google my problem, I found that it could work by using @staticmethod, but I would like to figure out another method without using @staticmethod. In func3, I didn't use @staticmethod, so it has a error: print(mycall.func3(3) ) TypeError: func3() takes exactly 1 argument (2 given) Does anyone know how to fix my problem? Thank you.

class mango:
    def func(self):
        a=8
        b=9
        return a+b       


class Apple:

    @staticmethod
    def func2(x,y):
        temp=mango()
        c=temp.func()
        z=x+y+c    
        return z


    def func3(val):
        temp = mango()
        d = temp.func()
        output = d + val
        return output


if __name__ == '__main__':

    print "Hello, Python!"

    a = []
    b =[]
    ans = Test(a,b)
    print(ans.cc)
    mycall = Apple()
    print(mycall.func2(2,3)) 
    print(mycall.func3(3) )
Whatlahuhu
  • 19
  • 2
  • 8
  • `def funcx(self, par1, par2)` add self to class method – Gang Feb 05 '17 at 02:05
  • BTW: when you call `mycall.func3(3)` then it treated almost like `func3(mycall, 3)` so you need `self` to get `mycall` – furas Feb 05 '17 at 02:21

3 Answers3

3

Whenever you create a method (instance's function), you need to give it a parameter self. It's how it works.

So, you need to change:

def func3(val):

to:

def func3(self, val):
0

Ideally you should be defining func3 function with @staticmethod decorator. But since you mentioned, you do not want to define it as static method, in order to fix the error you may define self as first argument in func3. Hence the definition of func3 should be like:

def func3(self, val):
# instead of > def func3(val):

Reason is, when you are making a call to the function like:

mycall.func3(3) 

Python passes reference of mycall as first argument and remaining parameters as the rest of arguments. This call will be received by function as:

func3(mycall, 3) 

Hence, in the above function call you are passing 2 parameters, but your current implementation func3 accepts only one. This is mentioned in error as well:

TypeError: func3() takes exactly 1 argument (2 given)

Why I suggested to define it as static method, i.e with @staticmethod decorator? You are currently not using any reference to the class' instance (i.e. self) within your function.


Must Read References:

Community
  • 1
  • 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
0

When you create methods inside classes (in this case, func2 and func3 inside Apple), Python treats the first argument as self, regardless of what you call that argument.

Essentially, what is happening is:

class MyClass(object):
    #stuff
    def myfunc(self, arg1):
        #do stuff
        return arg1

Then, in if __name__ == '__main__':

mycall = MyClass()
print(mycall.myfunc(5)) #In this instance, 5 is passed as arg1 

So in order to fix your code, try:

def func3(self, val):
        temp = mango()
        d = temp.func()
        output = d + val
        return output

It's worth mentioning that if you aren't using the instance itself, then you might as well use @staticmethod.

Read more: Why do you need explicitly have the "self" argument into a Python method? .

Community
  • 1
  • 1
K-man
  • 353
  • 1
  • 13