python中反射用法实例
作者:songguo 发布时间:2023-04-10 09:13:49
标签:python,反射,用法
本文实例讲述了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程序设计有所帮助。


猜你喜欢
- 在setup()钩子函数中调用父组件<template><div> &nb
- RIFF file formatRIFF全称为资源互换文件格式(Resources Interchange File Format),是Wi
- 前言:大家跟我一起念,Python * 好,跟着本宝宝用Python抢火车票首先我们需要splinter安装:pip install spli
- 库的管理1、库的管理创建、修改、删除1、库的创建CREATE DATABASE UF NOT EXISTS books;2、库的修改库名一般
- 1. 常见命令连接本地数据库与远程数据库(172.16.xx.xx:3306):mysql -h localhost -u root -p1
- 根据一个爬取豆瓣电影排名的小应用,来简单使用etree和request库。etree使用xpath语法。import requestsimp
- 如果使用的是matplotlib绘图,可以通过以下命令更改图片的大小:%matplotlib linline如果是 plt.figure(f
- 照片尺寸 单位:cm1X1.5 (1寸) 2.6*3.9 一寸2.5*3.5 1.5X2 (2寸) 3.8*
- 前言在任何编程语言中,代码需要根据不同的条件在给定的输入中做不同的决定和执行相应的动作。例如,在一个游戏中,如果玩家生命点为0,游戏结束。在
- 捣鼓了一天一直报错:Error running ‘mypython': Can't run remote python in
- 下列语句部分是Mssql语句,不可以在access中使用。SQL语句分类:DDL—数据定义语言(CREATE,ALTER,DROP,DECL
- 本文实例讲述了Yii2中SqlDataProvider用法。分享给大家供大家参考,具体如下:第一种方法:$totalCount = Yii:
- 1、问题描述:在学习北京大学曹健老师的tensorflow2.0笔记的时候,遇到了[Errno 2] No such file or dir
- Python中try块可以捕获测试代码块中的错误。except块可以处理错误。finally块可以执行代码,而不管try-和except块的
- 你是否对获得MySQL数据库与表的最基本命令的实际操作感到十分头疼?如果是这样子的话,以下的文章将会给你相应的解决方案,以下的文
- 原文:Unobtrusive Ajax。今天才看见的一个Presentation,是Jesse Skinner在06年10月发表的。虽然题目
- 结论:copy复制会比等号复制慢。但是copy复制为值复制,改变原切片的值不会影响新切片。而等号复制为指针复制,改变原切片或新切片都会对另一
- DJANGO_SETTINGS_MODULE使用Django时要通知Django当前使用的是哪个配置文件。可以改变环境变量 DJANGO_S
- 实例如下所示:#!/usr/bin/python# -*- coding: UTF-8 -*-import smtplibimport em
- 问题:mybatis返回的null类型数据消失,导致前端展示出错思路:如果查询出的结果是空值,应当转换成空字符串。当然在前端也能进行判断,但