Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: __init__() takes exactly 2 arguments (3 given)

Tags:

python

I am writing a program to find adapters, and have made a class called 'Adapter'. When I pass in two arguments IDLE gives me an error saying I passed in three! Here is the code and stack trace:

#This is the adapter class for the adapter finder script

class Adapter:
    side1 = (None,None)
    side2 = (None,None)
    '''The class that holds both sides of the adapter'''
    def __init__((pType1,pMF1),(pType2,pMF2)):
        '''Initiate the adapter.

        Keyword Arguments:
        pType1 -- The passed type of one side of the adapter. ex: BNC, RCA
        pMF1 -- The passed gender of pType1. ex: m, f

        pType2 -- The passed type of one side of the adapter. ex: BNC, RCA
        pMF2 -- The passed gender of pType2. ex: m, f

        '''

        print 'assigining now'
        side1 = (pType1,pMF1)
        print side1
        side2 = (pType2,pMF2)
        print side2

sideX = ('rca','m')
sideY = ('bnc','f')

x = Adapter(sideX,sideY)
print x.side1
print x.side2

Error: Traceback (most recent call last): File "C:\Users\Cody\Documents\Code\Python\Adapter Finder\adapter.py", line 28, in <module> x = Adapter(sideX,sideY) TypeError: __init__() takes exactly 2 arguments (3 given)

I don't understand what the problem is because I've only entered two args!

Edit: I'm new to the python language, though I know Java. I'm using this page as a tutorial: http://docs.python.org/tutorial/classes.html

like image 614
codysehl Avatar asked Jun 25 '10 01:06

codysehl


1 Answers

Yes, the OP missed the self, but I don't even know what those tuples-as-arguments mean and I'm intentionally not bothering to figure it out, it's just a bad construction.

Codysehi, please contrast your code with:

class Adapter:
    def __init__(self, side1, side2):
        self.side1 = side1
        self.side2 = side2

sideX = ('rca', 'm')
sideY = ('bnc', 'f')
x = Adapter(sideX, sideY)

and see that it is both more readable, and does what I think you intend.

like image 59
msw Avatar answered Sep 28 '22 08:09

msw