向python对象动态添加方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def barFighters( self ):
print "barFighters"
a.barFighters = barFighters
a.barFighters()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: barFighters() takes exactly 1 argument (0 given)
import types
a.barFighters = types.MethodType( barFighters, a )
a.barFighters()
barFighters

>

引用:
http://stackoverflow.com/questions/972/adding-a-method-to-an-existing-object-instance