inspect模块是针对模块,类,方法,功能等对象提供些有用的方法。例如可以帮助我们检查类的内容,检查方法的代码,提取和格式化方法的参数等。

import inspect
import os class Test(object):
"""Test Class """ def test(self):
self.fuc = lambda x: x class Testone(Test):
pass print(inspect.ismodule(os)) # True
print(inspect.isclass(Test)) # True
print(inspect.getdoc(Test)) # Test Class print(inspect.getsourcefile(Test)) # 文件路径
print(inspect.getsourcelines(Test)) # 代码块,每行一个元素,组成数组
print(inspect.getsource(Test)) # 获取代码块原本内容,带缩进

 打印全局中的变量

#打印全局变量中的模块对象
myglobals = {}
myglobals.update(globals())
modules = [value
for key, value in myglobals.items()
if inspect.ismodule(value)]
print(modules) 输出结果:
[<module 'builtins' (built-in)>, <module 'inspect' from 'D:\\python\\python\\lib\\inspect.py'>, <module 'os' from 'D:\\python\\python\\lib\\os.py'>]

 查看类和类对象有哪些方法可以调用 

# 查看类的可调用方法
for name, value in inspect.getmembers(Test, callable):
print(" Callable:", name) for name, value in inspect.getmembers(Test(), callable):
print(" Instance Callable:", name)

获取栈的全部调用信息

def debug():
import inspect
print(inspect.stack()[0][3])
print(inspect.stack()[1][3])
caller_name = inspect.stack()[1][3]
# print("[DEBUG]: enter {}()".format(caller_name) ) def say_hello():
debug()
# print("hello!") def say_goodbye():
debug()
# print("goodbye!") if __name__ == '__main__':
say_hello()
say_goodbye() 运行结果:
debug
say_hello
debug
say_goodbye

  

  1. inspect.ismodule(object): 是否为模块

  2. inspect.isclass(object):是否为类

  3. inspect.ismethod(object):是否为方法(bound method written in python)

  4. inspect.isfunction(object):是否为函数(python function, including lambda expression)

  5. inspect.isgeneratorfunction(object):是否为python生成器函数

  6. inspect.isgenerator(object):是否为生成器

  7. inspect.istraceback(object): 是否为traceback

  8. inspect.isframe(object):是否为frame

  9. inspect.iscode(object):是否为code

  10. inspect.isbuiltin(object):是否为built-in函数或built-in方法

  11. inspect.isroutine(object):是否为用户自定义或者built-in函数或方法

  12. inspect.isabstract(object):是否为抽象基类

  13. inspect.ismethoddescriptor(object):是否为方法标识符

  14. inspect.isdatadescriptor(object):是否为数字标识符,数字标识符有__get__ 和__set__属性; 通常也有__name__和__doc__属性

  15. inspect.isgetsetdescriptor(object):是否为getset descriptor

  16. inspect.ismemberdescriptor(object):是否为member descriptor

  

 

Python标准库--inspect的更多相关文章

  1. Python标准库inspect

    inspect模块用于收集python对象的信息,可以获取类或函数的参数的信息,源码,解析堆栈,对对象进行类型检查等等,有几个好用的方法: getargspec(func) 返回一个命名元组ArgSp ...

  2. python第六天 函数 python标准库实例大全

    今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...

  3. 转--Python标准库之一句话概括

    作者原文链接 想掌握Python标准库,读它的官方文档很重要.本文并非此文档的复制版,而是对每一个库的一句话概括以及它的主要函数,由此用什么库心里就会有数了. 文本处理 string: 提供了字符集: ...

  4. Python 标准库一览(Python进阶学习)

    转自:http://blog.csdn.net/jurbo/article/details/52334345 写这个的起因是,还是因为在做Python challenge的时候,有的时候想解决问题,连 ...

  5. Python标准库笔记(9) — functools模块

    functools 作用于函数的函数 functools 模块提供用于调整或扩展函数和其他可调用对象的工具,而无需完全重写它们. 装饰器 partial 类是 functools 模块提供的主要工具, ...

  6. python 标准库大全

    python 标准库 文本 string:通用字符串操作 re:正则表达式操作 difflib:差异计算工具 textwrap:文本填充 unicodedata:Unicode字符数据库 string ...

  7. Python标准库14 数据库 (sqlite3)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python自带一个轻量级的关系型数据库SQLite.这一数据库使用SQL语言.S ...

  8. python标准库00 学习准备

    Python标准库----走马观花 python有一套很有用的标准库.标准库会随着python解释器一起安装在你的电脑上的.它是python的一个组成部分.这些标准库是python为你准备的利器,可以 ...

  9. Python标准库:内置函数hasattr(object, name)

    Python标准库:内置函数hasattr(object, name) 本函数是用来判断对象object的属性(name表示)是否存在.如果属性(name表示)存在,则返回True,否则返回False ...

随机推荐

  1. RedHat 6.4源码方式安装mysql5.5

    参考文档:http://dev.mysql.com/downloads/file/?id=463397 下载地址http://dev.mysql.com/doc/refman/5.7/en/ 文档地址 ...

  2. 二维数组展示到DataGridView(c#)

    窗体程序中二维数组展示到DataGridView public void TwoDArrayShowINDatagridview(string[,] arr) { DataTable dt = new ...

  3. c++下各种编码格式转换的方法

    1.利用c++11新特性 std::wstring_convert配合std::codecvt模板类 作者:Gomo Psivarh链接:https://www.zhihu.com/question/ ...

  4. csu 1947 三分

    题意: 长者对小明施加了膜法,使得小明每天起床就像马丁的早晨一样. 今天小明早上6点40醒来后发现自己变成了一名高中生,这时马上就要做早操了,小明连忙爬起来 他看到操场密密麻麻的人,突然灵光一闪想到了 ...

  5. POJ 2976 Dropping tests 【01分数规划+二分】

    题目链接:http://poj.org/problem?id=2976 Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  6. 用c#语言编写银行利率

    sing System;using System.Collections.Generic;using System.Linq;using System.Text; namespace ConsoleA ...

  7. 微信小程序全局/页面配置

    flex布局 父元素 display:flex; flex-direction: row; justify-content:space-between 补充 flex-direction属性决定主轴的 ...

  8. lua浮点数取整

    向下取整 math.floor(num) 向上取整 math.ceil(num) 取整取余 math.modf(num) 测试 num = 12.4 print(math.floor(num)) 12 ...

  9. Django模板简介

    在settings.py中有个TEMPLATES的设置,其中BACKEND用来配置Django模板引擎, DIRS 定义了一个目录列表,模板引擎按列表顺序搜索这些目录以查找模板源文件 一般我们都会把模 ...

  10. 查看系统PCI设备

    # lspci Host bridge:主板 VGA compatible controller:VGA显卡设备 Class 0403:声卡设备 USB Controller:USB接口设备 SATA ...