有趣的是,“正好取一个参数(给定两个)”Python错误

时间:2022-11-30 22:54:49

For the error:

错误:

TypeError: takes exactly 1 argument (2 given)

With the following class method:

使用以下类方法:

def extractAll(tag):
   ...

and calling it:

和调用它:

e.extractAll("th")

The error seems very odd when I'm giving it 1 argument, the method should take only 1 argument, but it's saying I'm not giving it 1 argument....I know the problem can be fixed by adding self into the method prototype but I wanted to know the reasoning behind the error.

错误似乎很奇怪当我给它1参数,方法应该只有一个参数,但它说我没有给它1参数....我知道这个问题可以通过将self添加到方法原型中来解决,但是我想知道错误背后的原因。

Am I getting it because the act of calling it via e.extractAll("th") also passes in self as an argument? And if so, by removing the self in the call, would I be making it some kind of class method that can be called like Extractor.extractAll("th")?

我得到它是因为通过e.extractAll(“th”)调用它的行为也将self传递为一个参数吗?如果是这样的话,通过在调用中删除self,我是否会使它成为某种类方法,可以像Extractor.extractAll(“th”)那样被调用?

6 个解决方案

#1


76  

The call

调用

e.extractAll("th")

for a regular method extractAll() is indeed equivalent to

对于常规方法,extractAll()实际上等于。

Extractor.extractAll(e, "th")

These two calls are treated the same in all regards, including the error messages you get.

这两个调用在所有方面都是相同的,包括您得到的错误消息。

If you don't need to pass the instance to a method, you can use a staticmethod:

如果不需要将实例传递给方法,可以使用staticmethod:

@staticmethod
def extractAll(tag):
    ...

which can be called as e.extractAll("th"). But I wonder why this is a method on a class at all if you don't need to access any instance.

它可以被称为e.extractAll(“th”)。但我想知道,如果不需要访问任何实例,为什么这是类上的一个方法。

#2


28  

If a non-static method is member of a class, you have to define it like that:

如果非静态方法是类的成员,则必须这样定义:

def Method(self, atributes..)

So, I suppose your 'e' is instance of some class with implemented method that tries to execute and has too much arguments.

因此,我假设您的“e”是某个类的实例,它执行的方法试图执行,并且有太多的参数。

#3


8  

Am I getting it because the act of calling it via e.extractAll("th") also passes in self as an argument?

我之所以得到它,是因为通过e.extractAll(“th”)的行为,也传递了自我作为一个论点吗?

Yes, that's precisely it. If you like, the first parameter is the object name, e that you are calling it with.

是的,这正是它。如果你喜欢,第一个参数是你调用它的对象名e。

And if so, by removing the self in the call, would I be making it some kind of class method that can be called like Extractor.extractAll("th")?

如果是这样的话,通过在调用中删除self,我是否会使它成为某种类方法,可以像Extractor.extractAll(“th”)那样被调用?

Not quite. A classmethod needs the @classmethod decorator, and that accepts the class as the first paramater (usually referenced as cls). The only sort of method that is given no automatic parameter at all is known as a staticmethod, and that again needs a decorator (unsurprisingly, it's @staticmethod). A classmethod is used when it's an operation that needs to refer to the class itself: perhaps instantiating objects of the class; a staticmethod is used when the code belongs in the class logically, but requires no access to class or instance.

不完全是。classmethod需要@classmethod decorator,它接受类作为第一个参数(通常引用为cls)。唯一一种完全没有自动参数的方法叫做staticmethod,它同样需要一个decorator(毫不奇怪,它是@staticmethod)。类方法用于需要引用类本身的操作:可能实例化类的对象;当代码在逻辑上属于类时,使用staticmethod,但是不需要访问类或实例。

But yes, both staticmethods and classmethods can be called by referencing the classname as you describe: Extractor.extractAll("th").

但是,可以通过引用您所描述的类名来调用staticmethods和classmethods: Extractor.extractAll(“th”)。

#4


3  

Yes, when you invoke e.extractAll(foo), Python munges that into extractAll(e, foo).

是的,当您调用e.extractAll(foo)时,Python会将其分解为extractAll(e, foo)。

From http://docs.python.org/tutorial/classes.html

从http://docs.python.org/tutorial/classes.html

the special thing about methods is that the object is passed as the first argument of the function. In our example, the call x.f() is exactly equivalent to MyClass.f(x). In general, calling a method with a list of n arguments is equivalent to calling the corresponding function with an argument list that is created by inserting the method’s object before the first argument.

方法的特殊之处在于对象作为函数的第一个参数被传递。在我们的示例中,调用x.f()完全等价于MyClass.f(x)。通常,调用带有n个参数列表的方法等价于用一个参数列表调用相应的函数,该参数列表是在第一个参数之前插入方法的对象。

Emphasis added.

重点补充道。

#5


3  

Summary (Some examples of how to define methods in classes in python)

摘要(关于如何在python中定义类的一些示例)

#!/usr/bin/env python   # (if running from bash)

class Class1(object):

    def A(self, arg1):
        print arg1
        # this method requires an instance of Class1   
        # can access self.variable_name, and other methods in Class1

    @classmethod
    def B(cls, arg1):
        cls.C(arg1)
        # can access methods B and C in Class1 

    @staticmethod
    def C(arg1):
        print arg1
        # can access methods B and C in Class1 
        # (i.e. via Class1.B(...) and Class1.C(...))

Example

例子

my_obj=Class1()

my_obj.A("1")
# Class1.A("2") # TypeError: method A() must be called with Class1 instance

my_obj.B("3")
Class1.B("4")
my_obj.C("5")
Class1.C("6")`

#6


1  

try using:

尝试使用:

def extractAll(self,tag):

attention to self

注意自我

#1


76  

The call

调用

e.extractAll("th")

for a regular method extractAll() is indeed equivalent to

对于常规方法,extractAll()实际上等于。

Extractor.extractAll(e, "th")

These two calls are treated the same in all regards, including the error messages you get.

这两个调用在所有方面都是相同的,包括您得到的错误消息。

If you don't need to pass the instance to a method, you can use a staticmethod:

如果不需要将实例传递给方法,可以使用staticmethod:

@staticmethod
def extractAll(tag):
    ...

which can be called as e.extractAll("th"). But I wonder why this is a method on a class at all if you don't need to access any instance.

它可以被称为e.extractAll(“th”)。但我想知道,如果不需要访问任何实例,为什么这是类上的一个方法。

#2


28  

If a non-static method is member of a class, you have to define it like that:

如果非静态方法是类的成员,则必须这样定义:

def Method(self, atributes..)

So, I suppose your 'e' is instance of some class with implemented method that tries to execute and has too much arguments.

因此,我假设您的“e”是某个类的实例,它执行的方法试图执行,并且有太多的参数。

#3


8  

Am I getting it because the act of calling it via e.extractAll("th") also passes in self as an argument?

我之所以得到它,是因为通过e.extractAll(“th”)的行为,也传递了自我作为一个论点吗?

Yes, that's precisely it. If you like, the first parameter is the object name, e that you are calling it with.

是的,这正是它。如果你喜欢,第一个参数是你调用它的对象名e。

And if so, by removing the self in the call, would I be making it some kind of class method that can be called like Extractor.extractAll("th")?

如果是这样的话,通过在调用中删除self,我是否会使它成为某种类方法,可以像Extractor.extractAll(“th”)那样被调用?

Not quite. A classmethod needs the @classmethod decorator, and that accepts the class as the first paramater (usually referenced as cls). The only sort of method that is given no automatic parameter at all is known as a staticmethod, and that again needs a decorator (unsurprisingly, it's @staticmethod). A classmethod is used when it's an operation that needs to refer to the class itself: perhaps instantiating objects of the class; a staticmethod is used when the code belongs in the class logically, but requires no access to class or instance.

不完全是。classmethod需要@classmethod decorator,它接受类作为第一个参数(通常引用为cls)。唯一一种完全没有自动参数的方法叫做staticmethod,它同样需要一个decorator(毫不奇怪,它是@staticmethod)。类方法用于需要引用类本身的操作:可能实例化类的对象;当代码在逻辑上属于类时,使用staticmethod,但是不需要访问类或实例。

But yes, both staticmethods and classmethods can be called by referencing the classname as you describe: Extractor.extractAll("th").

但是,可以通过引用您所描述的类名来调用staticmethods和classmethods: Extractor.extractAll(“th”)。

#4


3  

Yes, when you invoke e.extractAll(foo), Python munges that into extractAll(e, foo).

是的,当您调用e.extractAll(foo)时,Python会将其分解为extractAll(e, foo)。

From http://docs.python.org/tutorial/classes.html

从http://docs.python.org/tutorial/classes.html

the special thing about methods is that the object is passed as the first argument of the function. In our example, the call x.f() is exactly equivalent to MyClass.f(x). In general, calling a method with a list of n arguments is equivalent to calling the corresponding function with an argument list that is created by inserting the method’s object before the first argument.

方法的特殊之处在于对象作为函数的第一个参数被传递。在我们的示例中,调用x.f()完全等价于MyClass.f(x)。通常,调用带有n个参数列表的方法等价于用一个参数列表调用相应的函数,该参数列表是在第一个参数之前插入方法的对象。

Emphasis added.

重点补充道。

#5


3  

Summary (Some examples of how to define methods in classes in python)

摘要(关于如何在python中定义类的一些示例)

#!/usr/bin/env python   # (if running from bash)

class Class1(object):

    def A(self, arg1):
        print arg1
        # this method requires an instance of Class1   
        # can access self.variable_name, and other methods in Class1

    @classmethod
    def B(cls, arg1):
        cls.C(arg1)
        # can access methods B and C in Class1 

    @staticmethod
    def C(arg1):
        print arg1
        # can access methods B and C in Class1 
        # (i.e. via Class1.B(...) and Class1.C(...))

Example

例子

my_obj=Class1()

my_obj.A("1")
# Class1.A("2") # TypeError: method A() must be called with Class1 instance

my_obj.B("3")
Class1.B("4")
my_obj.C("5")
Class1.C("6")`

#6


1  

try using:

尝试使用:

def extractAll(self,tag):

attention to self

注意自我