本文实例讲述了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数据结构与算法教程》、《Python加密解密算法与技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

02-02 05:51