动态的对类的方法、属性进行操作
'''
反射
getattr 获得该对象的方法
hasattr 判断对象有没有该方法
setattr
delattr
'''
class People(object):
def __init__(self,name):
self.name = name
def eat(self):
print(self.name + ' eating')
d = People('xiaoming')
s='eat'
if hasattr(d,s):
getattr(d,s)()
def play(self):
print('play...')
setattr(d,'myplay',play)#添加方法
d.myplay(d)