Python: __init__() takes exactly 2 arguments (3 given)
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 = ('bn开发者_开发问答c','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
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.
Method calls automatically get a 'self' parameter as the first argument, so make __init__
() look like:
def __init__(self, (pType1,pMF1),(pType2,pMF2)):
This is usually implicit in other languages, in Python it must be explicit. Also note that it's really just a way of informing the method of the instance it belongs to, you don't have to call it 'self'.
Your __init__
should look like this:
def __init__(self,(pType1,pMF1),(pType2,pMF2)):
Looks like this is the way Python says hello to everyone learning the language. Kind of Python's first bite.
You have to specify self in instance methods as first argument. So it should be.
def __init__( self, (pType1,pMF1),(pType2,pMF2)):
class Adapter:
side1 = (None,None)
side2 = (None,None)
'''The class that holds both sides of the adapter'''
def __init__(self,(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'
self.side1 = (pType1,pMF1)#i have changed from side1 to self.side1
print self.side1#i have changed from side1 to self.side1
self.side2 = (pType2,pMF2)#i have changed from side1 to self.side2
print self.side2#i have changed from side1 to self.side2
sideX = ('rca','m')
sideY = ('bnc','f')
x = Adapter(sideX,sideY)
print x.side1
print x.side2
see the output below.
精彩评论