Adding method to object

Aahz aahz at pythoncraft.com
Fri Dec 5 22:04:33 EST 2003


In article <pan.2003.12.03.09.44.12.160177 at thomas-guettler.de>,
Thomas Guettler <guettli at thomas-guettler.de> wrote:
>
>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()

Try ``Foo.incr=incr``
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote 
programs, then the first woodpecker that came along would destroy civilization.




More information about the Python-list mailing list