本文實例講述了Python反射用法。分享給大家供大家參考,具體如下:
class Person: def __init__(self): self.name = "zjgtan" def getName(self): return self.name
反射的簡單含義:
通過類名獲得類的實例對象
通過方法名得到方法,實現(xiàn)調(diào)用
反射方法一:
from person import PersontheObj = globals()["Person"]()print theObj.getName()
反射方法二:
module = __import__("person")theObj = getattr(module, "Person")()print theObj.getName()更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
新聞熱點
疑難解答