其實(shí)就是動(dòng)態(tài)的獲取類的屬性和方法class Person: def __init__(self): self.name = "zjgtan" def getName(self): return self.name反射的簡(jiǎn)單含義:
通過(guò)類名獲得類的實(shí)例對(duì)象
通過(guò)方法名得到方法,實(shí)現(xiàn)調(diào)用
反射方法一:
from person import PersontheObj = globals()["Person"]()PRint theObj.getName()反射方法二:
module = __import__("person")theObj = getattr(module, "Person")()print theObj.getName()
|
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注