必须使用instance作为第一个参数调用unbound方法(没有任何内容)

Toa*_*alp 2 python inheritance class

我试图从一个继承移动所有类.我写了这个小脚本:

class c1:
    def move():
        x+=1
        y+=1
class c2(c1):
    y=1
    x=2
c=c2
c.move()
print(str(c.x)+" , "+str(c.y))
Run Code Online (Sandbox Code Playgroud)

当我运行它我得到:

Traceback (most recent call last):   File "/home/tor/Workspace/try.py", line 9, in <module>
     c.move() TypeError: unbound method move() must be called with c2 instance as first argument (got nothing instead) [Finished in 0.1s
with exit code 1]
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

Mar*_*cin 9

  1. 你没有实例化任何东西

  2. 所有方法必须至少采用一个参数,传统上称之为self.

  3. 您需要self访问对象字段.您的代码现在修改了该范围内不存在的局部变量.