unbound method must be called with instance as fir

2019-02-19 11:13发布

Im trying to move all classes from one Inheritance. I wrote this tiny script:

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))

when i run it i get:

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]

what did I do wrong?

1条回答
Bombasti
2楼-- · 2019-02-19 12:10
  1. You do not instantiate anything

  2. All methods must take at least one parameter, traditionally called self.

  3. You need self to access object fields. Your code right now modifies local variables which do not exist in that scope.

查看更多
登录 后发表回答