One of the great features of Python is the ability to define methods outside of classes. For example, we can define a function which increments the attribute
x and add it to a
def incx(self): self.x += 1 class Point(object): def __init__(self, x): self.x = x incx = incx
We can then create a point at the origin and increment
In : p = Point(0) In : p.incx() In : print p.x 1
The same code which defines
Point continues to work if we move
incx to another file, say
demo.py, and import it using
from demo import incx.
$ python bad.py Traceback (most recent call last): File "bad.py", line 10, in <module> p.incx() TypeError: incx() takes exactly one argument (0 given)
Read on to learn about instance methods and see how I fixed this.