Python: __init__()只接受2个参数(3个给定)

时间:2021-03-05 23:17:39

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:

我正在编写一个程序来查找适配器,并创建了一个名为“适配器”的类。当我传递两个参数时,IDLE给了我一个错误,说我通过了三个!下面是代码和堆栈跟踪:

#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)

错误:Traceback(最近的调用):文件“C:\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \适配器\适配器。 <模块> x =适配器(sideX,sideY)类型错误:__init__()只接受2个参数(给定)

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

我不明白问题是什么,因为我只进了两个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

编辑:我是python语言的新手,尽管我知道Java。我使用这个页面作为教程:http://docs.python.org/tutorial/classes.html。

4 个解决方案

#1


19  

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.

是的,OP错过了自我,但我甚至不知道那些tuplas -as-arguments是什么意思,我故意不去想它,它只是一个糟糕的构造。

Codysehi, please contrast your code with:

Codysehi,请将您的代码与:

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.

看它的可读性更好,我认为你想要的。

#2


13  

Method calls automatically get a 'self' parameter as the first argument, so make __init__() look like:

方法调用自动获得一个“self”参数作为第一个参数,所以要使__init__()看起来像:

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'.

这通常隐含在其他语言中,在Python中它必须是显式的。还要注意,这实际上只是一种通知方法的方法,它属于,你不必称它为“self”。

#3


5  

Your __init__ should look like this:

你的__init__应该是这样的:

def __init__(self,(pType1,pMF1),(pType2,pMF2)):

#4


4  

Looks like this is the way Python says hello to everyone learning the language. Kind of Python's first bite.

看起来这就是Python向所有学习语言的人打招呼的方式。这是Python的第一口。

You have to specify self in instance methods as first argument. So it should be.

您必须在实例方法中指定self作为第一个参数。所以它应该是。

  def __init__( self, (pType1,pMF1),(pType2,pMF2)):

#1


19  

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.

是的,OP错过了自我,但我甚至不知道那些tuplas -as-arguments是什么意思,我故意不去想它,它只是一个糟糕的构造。

Codysehi, please contrast your code with:

Codysehi,请将您的代码与:

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.

看它的可读性更好,我认为你想要的。

#2


13  

Method calls automatically get a 'self' parameter as the first argument, so make __init__() look like:

方法调用自动获得一个“self”参数作为第一个参数,所以要使__init__()看起来像:

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'.

这通常隐含在其他语言中,在Python中它必须是显式的。还要注意,这实际上只是一种通知方法的方法,它属于,你不必称它为“self”。

#3


5  

Your __init__ should look like this:

你的__init__应该是这样的:

def __init__(self,(pType1,pMF1),(pType2,pMF2)):

#4


4  

Looks like this is the way Python says hello to everyone learning the language. Kind of Python's first bite.

看起来这就是Python向所有学习语言的人打招呼的方式。这是Python的第一口。

You have to specify self in instance methods as first argument. So it should be.

您必须在实例方法中指定self作为第一个参数。所以它应该是。

  def __init__( self, (pType1,pMF1),(pType2,pMF2)):