原型: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