本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》及《》
希望本文所述对大家Python程序设计有所帮助。
如对本文有疑问, 点击进行留言回复!!
从零开始的PYTHON3摸鱼(二)windows如何运行python,编辑器选择
网友评论