Toalp
Toalp

Reputation: 362

unbound method must be called with instance as first argument (got nothing instead)

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?

Upvotes: 3

Views: 19374

Answers (1)

Marcin
Marcin

Reputation: 49816

  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.

Upvotes: 8

Related Questions