原型:getattr(对象,方法名)
#coding=utf-8 class pyMethod(object): def outstr(self): print('this is string') def outint(self): print('this is number') def outdate(self): print('this is date') if __name__=="__main__": str = 'int' getattr(pyMethod(),'out%s'%str)() str = 'str' getattr(pyMethod(),'out%s'%str)() str = 'date' getattr(pyMethod(),'out%s'%str)()
getattr(pyMethod(),'out%s'%str)() 注意pyMethod()和最后的() 这里之因此这么写pyMethod()加括号是实例化类对象,最后的括号,由于getattr函数反射后,是一个方法对象。
运行结果:python
C:\Python27\python.exe D:/weixin/python_getattr.py this is number this is string this is date Process finished with exit code 0
Linux and python学习交流1,2群已满.函数
Linux and python学习交流3群新开,欢迎加入,一块儿学习.qq 3群:563227894学习
不前进,不倒退,中止的状态是没有的.this
一块儿进步,与君共勉,spa