inspect — 审查存活对象

源代码: Lib/inspect.py


The inspect 模块提供几个有用函数以帮助获取有关存活对象 (譬如:模块、类、方法、函数、回溯、框架对象及代码对象) 的信息。例如,它可以帮助审查类的内容、检索方法的源代码、提取和格式化函数的自变量列表,或获取显示详细回溯所需的所有信息。

此模块提供 4 种主要服务:类型校验、获取源代码、审查类和函数及审核解释器堆栈。

类型和成员

The getmembers() 函数检索对象 (譬如:类或模块) 的成员。名称以 is 开头的函数主要以第 2 自变量的方便选择形式提供对于 getmembers() . They also help you determine when you can expect to find the following special attributes (see Import-related attributes on module objects for module attributes):

类型

属性

描述

class

__doc__

文档编制字符串

__name__

name with which this class was defined

__qualname__

合格名称

__module__

name of module in which this class was defined

__type_params__

A tuple containing the type parameters of a generic class

方法

__doc__

文档编制字符串

__name__

name with which this method was defined

__qualname__

合格名称

__func__

function object containing implementation of method

__self__

instance to which this method is bound, or None

__module__

name of module in which this method was defined

function

__doc__

文档编制字符串

__name__

name with which this function was defined

__qualname__

合格名称

__code__

code object containing compiled function bytecode

__defaults__

tuple of any default values for positional or keyword parameters

__kwdefaults__

mapping of any default values for keyword-only parameters

__globals__

global namespace in which this function was defined

__builtins__

builtins namespace

__annotations__

mapping of parameters names to annotations; "return" key is reserved for return annotations.

__type_params__

A tuple containing the type parameters of a generic function

__module__

name of module in which this function was defined

traceback

tb_frame

在此级别的帧对象

tb_lasti

index of last attempted instruction in bytecode

tb_lineno

Python 源代码中的当前行号

tb_next

next inner traceback object (called by this level)

frame

f_back

next outer frame object (this frame’s caller)

f_builtins

builtins namespace seen by this frame

f_code

在此帧中要执行的代码对象

f_globals

global namespace seen by this frame

f_lasti

index of last attempted instruction in bytecode

f_lineno

Python 源代码中的当前行号

f_locals

local namespace seen by this frame

f_trace

tracing function for this frame, or None

code

co_argcount

number of arguments (not including keyword only arguments, * or ** args)

co_code

原生编译字节码的字符串

co_cellvars

tuple of names of cell variables (referenced by containing scopes)

co_consts

tuple of constants used in the bytecode

co_filename

name of file in which this code object was created

co_firstlineno

number of first line in Python source code

co_flags

bitmap of CO_* flags, read more here

co_lnotab

encoded mapping of line numbers to bytecode indices

co_freevars

tuple of names of free variables (referenced via a function’s closure)

co_posonlyargcount

number of positional only arguments

co_kwonlyargcount

number of keyword only arguments (not including ** arg)

co_name

定义此代码对象的名称

co_qualname

fully qualified name with which this code object was defined

co_names

tuple of names other than arguments and function locals

co_nlocals

number of local variables

co_stacksize

virtual machine stack space required

co_varnames

tuple of names of arguments and local variables

generator

__name__

名称

__qualname__

合格名称

gi_frame

frame

gi_running

is the generator running?

gi_code

code

gi_yieldfrom

object being iterated by yield from ,或 None

async generator

__name__

名称

__qualname__

合格名称

ag_await

object being awaited on, or None

ag_frame

frame

ag_running

is the generator running?

ag_code

code

协程

__name__

名称

__qualname__

合格名称

cr_await

object being awaited on, or None

cr_frame

frame

cr_running

is the coroutine running?

cr_code

code

cr_origin

where coroutine was created, or None 。见 sys.set_coroutine_origin_tracking_depth()

builtin

__doc__

文档编制字符串

__name__

original name of this function or method

__qualname__

合格名称

__self__

instance to which a method is bound, or None

3.5 版改变: 添加 __qualname__ and gi_yieldfrom 属性到生成器。

The __name__ attribute of generators is now set from the function name, instead of the code name, and it can now be modified.

3.7 版改变: 添加 cr_origin 属性到协程。

3.10 版改变: 添加 __builtins__ 属性到函数。