Adding method to object

Thomas Guettler guettli at thomas-guettler.de
Wed Dec 3 04:44:12 EST 2003


Hi!

How can I add a method to an object.
This code does not work:

class Foo:
    def __init__(self):
        self.counter=0

f=Foo()

def incr(self):
    self.counter+=1

f.incr=incr

f.incr()

===> python extend.py
Traceback (most recent call last):
  File "extend.py", line 12, in ?
    f.incr()
TypeError: incr() takes exactly 1 argument (0 given)





More information about the Python-list mailing list