inspect --- Inspect live objects — Python 3.15.2 documentation
Navigation index modules | next | previous | Python » 3.15.2 documentation » The Python standard library » Python Runtime Services » inspect --- Inspect live objects | Theme | inspect --- Inspect live objects ¶ Source code: 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 Attribute Description 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 None __module__ name of module in which this method was defined function __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; "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 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 None if the frame is of a regular function f_trace tracing function for this frame, or None 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_* flags, read more here 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 yield from , or None gi_state state of the generator, one of GEN_CREATED , GEN_RUNNING , GEN_SUSPENDED , or GEN_CLOSED async generator __name__ name __qualname__ qualified name ag_await object being awaited on, or None ag_frame frame ag_running is the generator running? ag_suspended is the generator suspended? ag_code code ag_state state of the async generator, one of AGEN_CREATED , AGEN_RUNNING , AGEN_SUSPENDED , or AGEN_CLOSED coroutine __name__ name __qualname__ qualified name cr_await object being awaited on, or None 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 None .
See sys.set_coroutine_origin_tracking_depth() cr_state state of the coroutine, one of CORO_CREATED , CORO_RUNNING , CORO_SUSPENDED , or CORO_CLOSED builtin __doc__ documentation string __name__ original name of this function or method __qualname__ qualified name __self__ instance to which a method is bound, or module object a function was defined in, or None Changed in version 3.5: Add __qualname__ and gi_yieldfrom attributes to generators.
The __name__ attribute of generators is now set from the function name, instead of the code name, and it can now be modified. Changed in version 3.7: Add cr_origin attribute to coroutines. Changed in version 3.10: Add __builtins__ attribute to functions. Changed in version 3.11: Add gi_suspended attribute to generators. Changed in version 3.11: Add cr_suspended attribute to coroutines. Changed in version 3.12: Add ag_suspended attribute to async generators.
Changed in version 3.14: Add f_generator attribute to frames. Changed in version 3.15: Add gi_state attribute to generators, cr_state attribute to coroutines, and ag_state attribute to async generators. 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 the value object of each member—is supplied, only members for which the predicate returns a true value are included.
Note 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.
Note 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. Added in version 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 th
The rest of this page is on the live site.