python中反射用法实例
作者:songguo 时间:2023-04-10 09:13:49
本文实例讲述了python中反射用法。分享给大家供大家参考。具体如下:
import sys, types,new
def _get_mod(modulePath):
try:
aMod = sys.modules[modulePath]
if not isinstance(aMod, types.ModuleType):
raise KeyError
except KeyError:
# The last [''] is very important!
aMod = __import__(modulePath, globals(), locals(), [''])
sys.modules[modulePath] = aMod
return aMod
def _get_func(fullFuncName):
"""Retrieve a function object from a full dotted-package name."""
# Parse out the path, module, and function
lastDot = fullFuncName.rfind(u".")
funcName = fullFuncName[lastDot + 1:]
modPath = fullFuncName[:lastDot]
aMod = _get_mod(modPath)
aFunc = getattr(aMod, funcName)
# Assert that the function is a *callable* attribute.
assert callable(aFunc), u"%s is not callable." % fullFuncName
# Return a reference to the function itself,
# not the results of the function.
return aFunc
def _get_Class(fullClassName, parentClass=None):
"""Load a module and retrieve a class (NOT an instance).
If the parentClass is supplied, className must be of parentClass
or a subclass of parentClass (or None is returned).
"""
aClass = _get_func(fullClassName)
# Assert that the class is a subclass of parentClass.
if parentClass is not None:
if not issubclass(aClass, parentClass):
raise TypeError(u"%s is not a subclass of %s" %
(fullClassName, parentClass))
# Return a reference to the class itself, not an instantiated object.
return aClass
def applyFuc(obj,strFunc,arrArgs):
objFunc = getattr(obj, strFunc)
return apply(objFunc,arrArgs)
def getObject(fullClassName):
clazz = _get_Class(fullClassName)
return clazz()
if __name__=='__main__':
aa=getObject("inetservices.services.company.Company")
bb=applyFuc(aa, "select", ['select * from ngsys2',None])
print bb
希望本文所述对大家的Python程序设计有所帮助。
标签:python,反射,用法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python金融数据可视化汇总
2023-04-12 21:27:41
![](https://img.aspxhome.com/file/2023/3/75133_0s.png)
Paddle模型性能分析工具Profiler定位瓶颈点优化程序详解
2021-12-29 23:58:06
Python Print实现在输出中插入变量的例子
2022-06-07 11:12:09
![](https://img.aspxhome.com/file/2023/3/103263_0s.jpg)
带你彻底搞懂python操作mysql数据库(cursor游标讲解)
2024-01-25 21:53:58
![](https://img.aspxhome.com/file/2023/4/83904_0s.png)
基于python生成英文版词云图代码实例
2023-06-24 23:43:14
![](https://img.aspxhome.com/file/2023/4/131354_0s.png)
python如何对链表操作
2023-08-18 05:40:46
python高级内置函数用法实例
2023-01-04 00:06:03
python常用模块详解
2021-05-24 05:16:55
![](https://img.aspxhome.com/file/2023/0/95700_0s.png)
Python和Excel的完美结合的常用操作案例汇总
2021-12-30 05:15:37
![](https://img.aspxhome.com/file/2023/3/109663_0s.png)
Google logo “我的中国”谷歌国际少年绘画大赛小学1-3年级
2008-12-19 12:26:00
![](https://img.aspxhome.com/file/UploadPic/200812/19/j1-3_01-72s.jpg)
通过python实现windows桌面截图代码实例
2023-11-19 03:46:27
在IE 浏览器中使用 jquery的fadeIn() 效果 英文字符字体加粗
2011-06-06 10:28:00
flask设置cookie
2022-03-19 21:13:01
初识Golang Mutex互斥锁的使用
2024-05-09 09:39:57
![](https://img.aspxhome.com/file/2023/7/127347_0s.jpg)
在Django的通用视图中处理Context的方法
2023-02-25 20:50:45
用表格帮你了解Python数据类型
2023-11-08 08:55:25
Centos7中MySQL数据库使用mysqldump进行每日自动备份的编写
2024-01-17 11:52:41
mysql事务和隔离级别底层原理浅析
2024-01-28 03:19:35
![](https://img.aspxhome.com/file/2023/1/101551_0s.png)
基于jQuery实现的立体文字渐变效果
2009-05-18 19:15:00
![](https://img.aspxhome.com/file/UploadPic/20095/18/pic-22s.jpg)
PHP PDOStatement::fetchColumn讲解
2023-06-06 09:17:20