Home > database >  How to solve python error: TypeError: __init__() takes exactly 4 arguments (5 given)
How to solve python error: TypeError: __init__() takes exactly 4 arguments (5 given)

Time:12-26

I am new to Python. Can someone please help me to solve this error?

My code is as following,

class First:
    def passThisArgs(self, a, b, c):
        return Second(self, a, b, c)

class Second:
    def __init__(self, a, b, c):
        print 'Values are :\n', a, b, c

newObj = First()
a = 5
b = 6
c = 7
newObj.passThisArgs(a, b, c)

I am getting this error:

Traceback (most recent call last):
  File "/root/PycharmProjects/abc.py", line 13, in <module>
    newObj.passThisArgs(a, b, c)
  File "/root/PycharmProjects/abc.py", line 3, in passThisArgs
    return Second(self, a, b, c)
TypeError: __init__() takes exactly 4 arguments (5 given)

I am using python 2.7 in Linux.

CodePudding user response:

You don't pass "self" to another class. Python will create a brand new object and pass that automatically as the self parameter to __init__. So:

        return Second(a,b,c)
  • Related