本文实例讲述了Python反射用法。分享给大家供大家参考,具体如下:
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()
希望本文所述对大家Python程序设计有所帮助。