类型错误:method() 需要 1 个位置参数,但给出了 2 个 - TypeError: method() takes 1 positional argument but 2 were given

语言: CN / TW / HK

问题:

If I have a class...如果我有一堂课...

class MyClass:

    def method(arg):
        print(arg)

...which I use to create an object... ...我用来创建一个对象...

my_object = MyClass()

...on which I call method("foo") like so... ...我像这样调用method("foo") ...

>>> my_object.method("foo")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: method() takes exactly 1 positional argument (2 given)

...why does Python tell me I gave it two arguments, when I only gave one? ...为什么 Python 告诉我我给了它两个参数,而我只给了一个参数?


解决方案:

参考一: https://stackoom.com/question/1cT69
参考二: TypeError: method() takes 1 positional argument but 2 were given
「其他文章」