Happy fun python snippit

published: 25 Aug 2003 | tags: python

An example of how to add a method to a class, and how to add to just an instance of object.

import new

# add method to all object of a class

class test:

def monkey(self):
    print "eek"

tta = test()
ttb = test()
tta.__class__.monkey = monkey
print "%r" % tta.monkey
print "%r" % ttb.monkey

# add to one instance 

class test2:

tt2a = test2()
tt2b = test2()

tt2a.monkey = new.instancemethod(monkey, tt2a, tt2a.__class__)

print "%r" % tt2a.monkey
# this will cause an exception
print "%r" % tt2b.monkey