3

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?

Toalp
  • 362
  • 1
  • 4
  • 15
  • 1
    You are not instantiating any class. –  Oct 18 '13 at 05:50
  • 4
    Did you mean to do `c = c2()` and `c.move()`? – David Robinson Oct 18 '13 at 05:50
  • 3
    You need to learn about Classes first: http://docs.python.org/2/tutorial/classes.html – Ashwini Chaudhary Oct 18 '13 at 05:51
  • 1
    The above tutorial is good for people already familiar with classes (perhaps in another language) and want to see how they work in python, it is not great to learn about classes from the very beginning. The whole concept of classes is confusing for most beginners, so it is possible to be off even after even after putting in effort. – Akavall Apr 30 '14 at 21:01

1 Answers1

8
  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.

Marcin
  • 48,559
  • 18
  • 128
  • 201