# coding=utf-8_
_author__ = "leaves"
import sys
import inspect2 as inspect
class InspectTest(object):
def __init__(self, value1):
self.value2 = value1
def test(self, name):
print self.value2
print '-------------1代码块(code)------------------'
test = InspectTest("HI......")
print test.test("111")
cc = test.test.func_code
print cc.co_argcount # 普通参数的总数,不包括*参数和**参数。
print cc.co_names # 局部变量名的元组
print cc.co_varnames # 所有的局部变量名的元组。
print cc.co_filename # 源代码所在的文件名
print cc.co_flags # 这是一个数值,每一个二进制位都包含了特定信息。较关注的是0b100(0x4)和0b1000(0x8),如果co_flags & 0b100 != 0,说明使用了*args参数;如果co_flags & 0b1000 != 0,说明使用了**kwargs参数。另外,如果co_flags & 0b100000(0x20) != 0,则说明这是一个生成器函数(generator function)。
print '-------------2栈帧(frame)------------------'
def test2(x, y):
f = inspect.currentframe()
print f.f_locals # 用在当前栈帧时与内建函数locals()相同,但你可以先获取其他帧然后使用这个属性获取那个帧的locals()。
print f.f_code # 栈帧对应的code对象
print f.f_back # 调用栈的前一帧
print f.f_globals # 用在当前栈帧时与内建函数globals()
test2(1, 2)
print '------------- 3追踪(traceback)------------------'
def div(x, y):
try:
return x / y
except:
tb = sys.exc_info()[2]
print tb
print tb.tb_lineno # 多少行出错了
div(1, 0)
print '------------- 4模块------------------'
class Test:
_b = 100
def test(self, x, y):
print inspect.getmodule(self)#返回self的定义所在的模块对象。
print inspect.getmembers(self) # dir()的扩展版,它会将dir()找到的名字对应的属性一并返回,形如[(name, value), ...]
print inspect.currentframe()
print inspect.getouterframes(inspect.currentframe())[0][0]
print inspect.getouterframes(inspect.currentframe())[0][1]
print inspect.getouterframes(inspect.currentframe())[1][0]
print inspect.getouterframes(inspect.currentframe())[1][1]
print inspect.getmodulename(inspect.getouterframes(inspect.currentframe())[1][1])
# print inspect.getmodulename(inspect.getouterframes(inspect.currentframe())[0][0])
print inspect.getfile(Test)#所在的模块的文件名|
print inspect.getsourcefile(Test)#所在的模块的源代码
t = Test()
t.test(1, 2)
输出:
-------------1代码块(code)------------------
HI......
None
2
('value2',)
('self', 'name')
G:/python/test/My_test/inspect.py
67
-------------2栈帧(frame)------------------
{'y': 2, 'x': 1, 'f': <frame object at 0x00000000025E2200>}
<code object test2 at 000000000240C4B0, file "G:/python/test/My_test/inspect.py", line 28>
<frame object at 0x0000000002366048>
{'test2': <function test2 at 0x00000000025F0828>, '__builtins__': <module '__builtin__' (built-in)>, '__file__': 'G:/python/test/My_test/inspect.py', 'inspect': <module 'inspect2' from 'G:\python\test\My_test\inspect2.pyc'>, '__package__': None, 'sys': <module 'sys' (built-in)>, 'cc': <code object test at 000000000235F530, file "G:/python/test/My_test/inspect.py", line 11>, '_author__': 'leaves', 'test': <__main__.InspectTest object at 0x00000000024FFB38>, '__name__': '__main__', 'InspectTest': <class '__main__.InspectTest'>, '__doc__': None}
------------- 3追踪(traceback)------------------
<traceback object at 0x00000000024C1748>
42
------------- 4模块------------------
G:/python/test/My_test/inspect.py
G:/python/test/My_test/inspect.py
<module '__main__' from 'G:/python/test/My_test/inspect.py'>
[('__doc__', None), ('__module__', '__main__'), ('_b', 100), ('test', <bound method Test.test of <__main__.Test instance at 0x00000000024C17C8>>)]
<frame object at 0x00000000024C2930>
<frame object at 0x00000000024C2930>
G:/python/test/My_test/inspect.py
<frame object at 0x0000000002366048>
G:/python/test/My_test/inspect.py
inspect