Python反射用法实例简析
本文实例讲述了Python反射用法。分享给大家供大家参考,具体如下:
classPerson: def__init__(self): self.name="zjgtan" defgetName(self): returnself.name
反射的简单含义:
通过类名获得类的实例对象
通过方法名得到方法,实现调用
反射方法一:
frompersonimportPerson theObj=globals()["Person"]() printtheObj.getName()
反射方法二:
module=__import__("person") theObj=getattr(module,"Person")() printtheObj.getName()
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python加密解密算法与技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。