一、sys.modules模块
sys.modules是一个全局字典,python启动后就将该字典加载在内存中,每当导入新的模块时sys.modules都将记录这些导入模块。字典sys.modules对于加载模块起到了缓冲的作用。
sys.modules拥有字典所拥有的一切方法。如:
1
2
3
4
5
6
7
8
9
|
import sys print (sys.modules[__name__]) print (sys.modules.values()) print (sys.modules.keys()) print (sys.modules.items()) #示例 print (sys.modules.get( "demo2" )) #输出 <module 'demo3' from 'G:\\BaiXXXYuan\\BaiXXXXYuanApi\\demo3.py' > |
二、inspect模块
inspect模块主要提供了四种用处:
1.对对象进行类型检查
2.获取源码
3.获取类或者函数的参数信息
4.解析堆栈
- inspect.getmembers(object[, predicate])
返回一个包含对象的所有成员(name, value)的列表。返回的内容比对象的dict包含的内容多,源码是通过dir()实现的。
predicate是一个可选的函数参数,只有predicate函数判断为True的成员才被返回。
- predicate一般调用以下16个方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
inspect.ismodule( object ): 是否为模块 inspect.isclass( object ):是否为类 inspect.ismethod( object ):是否为方法(bound method written in python) inspect.isfunction( object ):是否为函数(python function, including lambda expression) inspect.isgeneratorfunction( object ):是否为python生成器函数 inspect.isgenerator( object ):是否为生成器 inspect.istraceback( object ): 是否为traceback inspect.isframe( object ):是否为frame inspect.iscode( object ):是否为code inspect.isbuiltin( object ):是否为built - in 函数或built - in 方法 inspect.isroutine( object ):是否为用户自定义或者built - in 函数或方法 inspect.isabstract( object ):是否为抽象基类 inspect.ismethoddescriptor( object ):是否为方法标识符 inspect.isdatadescriptor( object ):是否为数字标识符,数字标识符有__get__ 和__set__属性; 通常也有__name__和__doc__属性 inspect.isgetsetdescriptor( object ):是否为getset descriptor inspect.ismemberdescriptor( object ):是否为member descriptor |
inspect其他方法
1
2
3
4
5
6
7
8
|
inspect.getdoc( object ): 获取 object 的documentation信息 inspect.getcomments( object ) inspect.getfile( object ): 返回对象的文件名 inspect.getmodule( object ):返回 object 所属的模块名 inspect.getsourcefile( object ): 返回 object 的python源文件名; object 不能使built - in 的module, class , mothod inspect.getsourcelines( object ):返回 object 的python源文件代码的内容,行号 + 代码行 inspect.getsource( object ):以string形式返回 object 的源代码 inspect.cleandoc(doc): |
三、python获取模块中所有类的实例
demo2文件
1
2
3
4
5
6
|
class A(): def __init__( self ): self .A = "A" def run( self ): print ( self .A) |
demo3文件
1
2
3
4
5
6
7
|
class D(): def __init__( self ): self .D = "D" def d( self ): print ( self .D) |
demo文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
from demo import A import demo2 import inspect import sys class B(A): def __init__( self ): self .B1 = "B" def run( self ): print ( self .B1) class C(A): def __init__( self ): self .C1 = "C" def run( self ): print ( self .C1) if __name__ = = '__main__' : print (sys.modules.get( "demo3" )) class_list = [] print (inspect.getmembers(sys.modules[__name__], inspect.isclass)) print (inspect.getmembers(sys.modules.get( "demo3" ), inspect.isclass)) for name, class_ in inspect.getmembers(sys.modules[__name__], inspect.isclass): class_list.append( class_ ) class_ ().run() print (class_list) #输出 <module 'demo3' from 'G:\\BaiGuoYuan\\BaiGuoYuanApi\\demo3.py' > [( 'A' , < class 'demo2.A' >), ( 'B' , < class '__main__.B' >), ( 'C' , < class '__main__.C' >)] [( 'D' , < class 'demo3.D' >)] A B C [< class 'demo2.A' >, < class '__main__.B' >, < class '__main__.C' >] |