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

前端 未结 6 1820
傲寒
傲寒 2020-12-02 12:19

For the error:

TypeError: takes exactly 1 argument (2 given)

With the following class method:

def extractAll(tag):
   ...
<         


        
6条回答
  •  攒了一身酷
    2020-12-02 12:43

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

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

    Emphasis added.

提交回复
热议问题