Python 'takes exactly 1 argument of 2 given ' Python error

  • 2020-05-17 05:54:09
  • OfStack

When Python is a beginner, it defines the receiving parameters of urlConfig. When the parameters are normally passed, the error problem of giving one more parameter appears.

After defining the class function, "'takes exactly 1 argument (2 given)' Python error" appears on the call.

After querying Interesting 'takes exactly 1 argument (2 given)' Python error, it turns out that in python, calling its class method with instance is equivalent to adding itself as the first parameter in the call. As follows:

a.method(k)

Is equivalent to:

a.method(a, k)

Therefore, when defining a function, the first parameter inside the function should be defined as self, such as:

class Person():

def method(self, k):

...

If you want to call it as a static function, you can do so by adding "@staticmethod" to the top of the defined function.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: