class Person: def __init__(self): self.name = "zjgtan" def getName(self): return self.name
反射的簡單含義:
通過類名獲得類的實例對象
通過方法名得到方法,實現調用
反射方法一:
from person import Person theObj = globals()["Person"]() print theObj.getName()
反射方法二:
module = __import__("person") theObj = getattr(module, "Person")() print theObj.getName()