相关疑难解决方法(0)

TypeError:method()占用1个位置参数,但给出了2个

如果我上课了......

class MyClass:

    def method(arg):
        print(arg)
Run Code Online (Sandbox Code Playgroud)

...我用来创建一个对象......

my_object = MyClass()
Run Code Online (Sandbox Code Playgroud)

......我就这样打电话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)
Run Code Online (Sandbox Code Playgroud)

...为什么Python告诉我我给了它两个参数,当我只给出一个?

python methods arguments self python-3.x

210
推荐指数
8
解决办法
41万
查看次数

标签 统计

arguments ×1

methods ×1

python ×1

python-3.x ×1

self ×1