网络编程
位置:首页>> 网络编程>> Python编程>> Python反射用法实例简析

Python反射用法实例简析

作者:zjgtan  发布时间:2021-10-15 20:28:34 

标签:Python,反射

本文实例讲述了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程序设计有所帮助。

来源:https://www.cnblogs.com/zjgtan/p/3539808.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com