有趣的“恰好需要 1 个参数(给出 2 个)”Python 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4909585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 18:04:19  来源:igfitidea点击:

Interesting 'takes exactly 1 argument (2 given)' Python error

python

提问by funk-shun

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 selfinto the method prototype but I wanted to know the reasoning behind the error.

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

Am I getting it because the act of calling it via e.extractAll("th") also passes in selfas an argument? And if so, by removing the selfin 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")吗?

采纳答案by Sven Marnach

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 canuse 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"). 但是我想知道如果您不需要访问任何实例,为什么这是一个类上的方法。

回答by payne

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.

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

Emphasis added.

加了重点。

回答by Daniel Roseman

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

我得到它是因为通过 e.extractAll("th") 调用它的行为也将 self 作为参数传入吗?

Yes, that's precisely it. If you like, the first parameter is the object name, ethat 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 @classmethoddecorator, and that accepts the classas 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装饰器,并接受作为第一个参数(通常引用为cls)。唯一一种完全没有自动参数的方法被称为静态方法,它同样需要一个装饰器(不出所料,它是@staticmethod)。当需要引用类本身的操作时使用类方法:可能是实例化类的对象;当代码在逻辑上属于类时使用静态方法,但不需要访问类或实例。

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

但是,是的,静态方法和类方法都可以通过引用您描述的类名来调用:Extractor.extractAll("th")

回答by Martin

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' 是某个类的实例,该类的实现方法试图执行并且有太多参数。

回答by Rui Lima

try using:

尝试使用:

def extractAll(self,tag):

attention to self

关注自己

回答by user2533809

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")`