inspect --- 檢視活動物件¶
原始碼:Lib/inspect.py
The inspect module provides several useful functions to help get
information about live objects such as modules, classes, methods, functions,
tracebacks, frame objects, and code objects. For example, it can help you
examine the contents of a class, retrieve the source code of a method, extract
and format the argument list for a function, or get all the information you need
to display a detailed traceback.
There are four main kinds of services provided by this module: type checking, getting source code, inspecting classes and functions, and examining the interpreter stack.
Types and members¶
The getmembers() function retrieves the members of an object such as a
class or module. The functions whose names begin with "is" are mainly
provided as convenient choices for the second argument to 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):
Type |
屬性 |
描述 |
|---|---|---|
class |
__doc__ |
documentation string |
__name__ |
name with which this class was defined |
|
__qualname__ |
qualified name |
|
__module__ |
name of module in which this class was defined |
|
__type_params__ |
A tuple containing the type parameters of a generic class |
|
method |
__doc__ |
documentation string |
__name__ |
name with which this method was defined |
|
__qualname__ |
qualified name |
|
__func__ |
function object containing implementation of method |
|
__self__ |
instance to which this
method is bound, or
|
|
__module__ |
name of module in which this method was defined |
|
函式 |
__doc__ |
documentation string |
__name__ |
name with which this function was defined |
|
__qualname__ |
qualified name |
|
__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;
|
|
__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 |
frame object at this level |
tb_lasti |
index of last attempted instruction in bytecode |
|
tb_lineno |
current line number in Python source code |
|
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 |
code object being executed in this frame |
|
f_globals |
global namespace seen by this frame |
|
f_lasti |
index of last attempted instruction in bytecode |
|
f_lineno |
current line number in Python source code |
|
f_locals |
local namespace seen by this frame |
|
f_generator |
returns the generator or
coroutine object that
owns this frame, or
|
|
f_trace |
tracing function for this
frame, or |
|
f_trace_lines |
indicate whether a tracing event is triggered for each source source line |
|
f_trace_opcodes |
indicate whether per-opcode events are requested |
|
clear() |
used to clear all references to local variables |
|
code(程式碼) |
co_argcount |
number of arguments (not including keyword only arguments, * or ** args) |
co_code |
string of raw compiled bytecode |
|
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_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 |
name with which this code object was defined |
|
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 |
|
co_lines() |
returns an iterator that yields successive bytecode ranges |
|
co_positions() |
returns an iterator of source code positions for each bytecode instruction |
|
replace() |
returns a copy of the code object with new values |
|
generator |
__name__ |
name |
__qualname__ |
qualified name |
|
gi_frame |
frame |
|
gi_running |
is the generator running? |
|
gi_suspended |
is the generator suspended? |
|
gi_code |
code(程式碼) |
|
gi_yieldfrom |
object being iterated by
|
|
async generator |
__name__ |
name |
__qualname__ |
qualified name |
|
ag_await |
object being awaited on,
or |
|
ag_frame |
frame |
|
ag_running |
is the generator running? |
|
ag_suspended |
is the generator suspended? |
|
ag_code |
code(程式碼) |
|
coroutine |
__name__ |
name |
__qualname__ |
qualified name |
|
cr_await |
object being awaited on,
or |
|
cr_frame |
frame |
|
cr_running |
is the coroutine running? |
|
cr_suspended |
is the coroutine suspended? |
|
cr_code |
code(程式碼) |
|
cr_origin |
where coroutine was
created, or |
|
builtin |
__doc__ |
documentation string |
__name__ |
original name of this function or method |
|
__qualname__ |
qualified name |
|
__self__ |
instance to which a
method is bound, or
|
在 3.5 版的變更: 新增產生器的 __qualname__ 和 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__ 屬性。
在 3.11 版的變更: 新增產生器的 gi_suspended 屬性。
在 3.11 版的變更: 新增協程的 cr_suspended 屬性。
在 3.12 版的變更: 新增非同步產生器的 ag_suspended 屬性。
在 3.14 版的變更: 新增 f_generator 屬性到 frame。
- inspect.getmembers(object[, predicate])¶
Return all the members of an object in a list of
(name, value)pairs sorted by name. If the optional predicate argument—which will be called with thevalueobject of each member—is supplied, only members for which the predicate returns a true value are included.備註
getmembers()will only return class attributes defined in the metaclass when the argument is a class and those attributes have been listed in the metaclass' custom__dir__().
- inspect.getmembers_static(object[, predicate])¶
Return all the members of an object in a list of
(name, value)pairs sorted by name without triggering dynamic lookup via the descriptor protocol, __getattr__ or __getattribute__. Optionally, only return members that satisfy a given predicate.備註
getmembers_static()may not be able to retrieve all members that getmembers can fetch (like dynamically created attributes) and may find members that getmembers can't (like descriptors that raise AttributeError). It can also return descriptor objects instead of instance members in some cases.在 3.11 版被加入.
- inspect.getmodulename(path)¶
Return the name of the module named by the file path, without including the names of enclosing packages. The file extension is checked against all of the entries in
importlib.machinery.all_suffixes(). If it matches, the final path component is returned with the extension removed. Otherwise,Noneis returned.Note that this function only returns a meaningful name for actual Python modules - paths that potentially refer to Python packages will still return
None.