我正在编写一个程序来查找适配器,并且创建了一个名为'Adapter'的类。当我传入两个参数时,IDLE给我一个错误,说我通过了三个!下面是代码和堆栈跟踪:Python:__init __()需要2个参数(给出3个)
#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
错误: 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)
我不明白是什么问题,因为我只进入有两个参数!
编辑:我是新来的Python语言,但我知道Java。 我使用这个页面作为教程:http://docs.python.org/tutorial/classes.html
您对__init__的定义是错误的。你正在使用什么教程?你为什么写这个?为什么你会忽略'self'变量?请包括您正在使用的教程的名称或链接。 – 2010-06-25 01:39:47
您在参数列表中使用元组表达式是非常不习惯的Python,会混淆很多人。我建议你不要这样做,特别是因为你只是把它们分配给其他东西。请不要使用非常奇怪但奇怪的Python构造,除非你有一个非常奇妙的理由。 – Omnifarious 2010-06-25 01:51:34
不同的人学习方式不同,你可能会发现diveintopython.org比docs.python.org更合适 – msw 2010-06-25 04:05:57