Add methods to a class at runtime?

Gerhard Häring gerhard.haering at
Sun Sep 1 17:47:48 CEST 2002

Robert Oschler wrote in comp.lang.python:
> [Glyph said:]
> "One of the eye-popping cool features of Python is the ability to
> change code and have existing instances update to use the new
> methods automatically. "
> Can someone point me to a code snippet shows how to do this?

>>> class Foo: pass
>>> foo = Foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: Foo instance has no attribute 'bar'
>>> def bar(self): print "bar"
>>> = bar
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    OpenPGP public key id AD24C930
public key fingerprint: 3FCC 8700 3012 0A9E B0C9  3667 814B 9CAA AD24 C930
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))

More information about the Python-list mailing list