inspect

Get useful information from live Python objects.

This module encapsulates the interface provided by the internal special attributes (func_*, co_*, im_*, tb_*, etc.) in a friendlier fashion. It also provides some help for examining source code and class layout.

Here are some of the useful functions provided by this module:

ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(),
isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(), isroutine() - check object types

getmembers() - get members of an object that satisfy a given condition

getfile(), getsourcefile(), getsource() - find an object’s source code getdoc(), getcomments() - get documentation on an object getmodule() - determine the module that an object came from getclasstree() - arrange classes so as to represent their hierarchy

getargspec(), getargvalues(), getcallargs() - get info about function arguments formatargspec(), formatargvalues() - format an argument spec getouterframes(), getinnerframes() - get info about frames currentframe() - get the current stack frame stack(), trace() - get info about frames on the stack or in a traceback

Functions

classify_class_attrs(cls) Return list of attribute-descriptor tuples.
cleandoc(doc) Clean up indentation from docstrings.
currentframe _getframe([depth]) -> frameobject
findsource(object) Return the entire source file and starting line number for an object.
formatargspec(args[, varargs, varkw, ...]) Format an argument spec from the 4 values returned by getargspec.
formatargvalues(args, varargs, varkw, locals) Format an argument spec from the 4 values returned by getargvalues.
getabsfile(object[, _filename]) Return an absolute path to the source or compiled file for an object.
getargs(co) Get information about the arguments accepted by a code object.
getargspec(func) Get the names and default values of a function’s arguments.
getargvalues(frame) Get information about arguments passed into a particular frame.
getblock(lines) Extract the block of code at the top of the given list of lines.
getcallargs(func, *positional, **named) Get the mapping of arguments to values.
getclasstree(classes[, unique]) Arrange the given list of classes into a hierarchy of nested lists.
getcomments(object) Get lines of comments immediately preceding an object’s source code.
getdoc(object) Get the documentation string for an object.
getfile(object) Work out which source or compiled file an object was defined in.
getframeinfo(frame[, context]) Get information about a frame or traceback object.
getinnerframes(tb[, context]) Get a list of records for a traceback’s frame and all lower frames.
getlineno(frame) Get the line number from a frame object, allowing for optimization.
getmembers(object[, predicate]) Return all members of an object as (name, value) pairs sorted by name.
getmodule(object[, _filename]) Return the module an object was defined in, or None if not found.
getmoduleinfo(path) Get the module name, suffix, mode, and module type for a given file.
getmodulename(path) Return the module name for a given file, or None.
getmro(cls) Return tuple of base classes (including cls) in method resolution order.
getouterframes(frame[, context]) Get a list of records for a frame and all higher (calling) frames.
getsource(object) Return the text of the source code for an object.
getsourcefile(object) Return the filename that can be used to locate an object’s source.
getsourcelines(object) Return a list of source lines and starting line number for an object.
indentsize(line) Return the indent size, in spaces, at the start of a line of text.
isabstract(object) Return true if the object is an abstract base class (ABC).
isbuiltin(object) Return true if the object is a built-in function or method.
isclass(object) Return true if the object is a class.
iscode(object) Return true if the object is a code object.
isdatadescriptor(object) Return true if the object is a data descriptor.
isframe(object) Return true if the object is a frame object.
isfunction(object) Return true if the object is a user-defined function.
isgenerator(object) Return true if the object is a generator.
isgeneratorfunction(object) Return true if the object is a user-defined generator function.
isgetsetdescriptor(object) Return true if the object is a getset descriptor.
ismemberdescriptor(object) Return true if the object is a member descriptor.
ismethod(object) Return true if the object is an instance method.
ismethoddescriptor(object) Return true if the object is a method descriptor.
ismodule(object) Return true if the object is a module.
isroutine(object) Return true if the object is any kind of function or method.
istraceback(object) Return true if the object is a traceback.
joinseq(seq)
namedtuple(typename, field_names[, verbose, ...]) Returns a new subclass of tuple with named fields.
stack([context]) Return a list of records for the stack above the caller’s frame.
strseq(object, convert[, join]) Recursively walk a sequence, stringifying each element.
trace([context]) Return a list of records for the stack below the current exception.
walktree(classes, children, parent) Recursive helper function for getclasstree().

Classes

ArgInfo
ArgSpec
Arguments
Attribute
BlockFinder() Provide a tokeneater() method to detect the end of a code block.
ModuleInfo
Traceback
attrgetter attrgetter(attr, ...) –> attrgetter object

Exceptions

EndOfBlock