cpython/Doc/library/inspect.rst

596 lines
26 KiB
ReStructuredText
Raw Normal View History

2007-08-15 11:28:01 -03:00
:mod:`inspect` --- Inspect live objects
=======================================
.. module:: inspect
:synopsis: Extract information and source code from live objects.
.. moduleauthor:: Ka-Ping Yee <ping@lfw.org>
.. sectionauthor:: Ka-Ping Yee <ping@lfw.org>
.. versionadded:: 2.1
The :mod:`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.
.. _inspect-types:
Types and members
-----------------
The :func:`getmembers` function retrieves the members of an object such as a
2008-03-03 16:37:55 -04:00
class or module. The sixteen functions whose names begin with "is" are mainly
2007-08-15 11:28:01 -03:00
provided as convenient choices for the second argument to :func:`getmembers`.
They also help you determine when you can expect to find the following special
attributes:
+-----------+-----------------+---------------------------+-------+
| Type | Attribute | Description | Notes |
+===========+=================+===========================+=======+
| module | __doc__ | documentation string | |
+-----------+-----------------+---------------------------+-------+
| | __file__ | filename (missing for | |
| | | built-in modules) | |
+-----------+-----------------+---------------------------+-------+
| class | __doc__ | documentation string | |
+-----------+-----------------+---------------------------+-------+
| | __module__ | name of module in which | |
| | | this class was defined | |
+-----------+-----------------+---------------------------+-------+
| method | __doc__ | documentation string | |
+-----------+-----------------+---------------------------+-------+
| | __name__ | name with which this | |
| | | method was defined | |
+-----------+-----------------+---------------------------+-------+
| | im_class | class object that asked | \(1) |
| | | for this method | |
+-----------+-----------------+---------------------------+-------+
| | im_func or | function object | |
| | __func__ | containing implementation | |
2007-08-15 11:28:01 -03:00
| | | of method | |
+-----------+-----------------+---------------------------+-------+
| | im_self or | instance to which this | |
| | __self__ | method is bound, or | |
2007-08-15 11:28:01 -03:00
| | | ``None`` | |
+-----------+-----------------+---------------------------+-------+
| function | __doc__ | documentation string | |
+-----------+-----------------+---------------------------+-------+
| | __name__ | name with which this | |
| | | function was defined | |
+-----------+-----------------+---------------------------+-------+
| | func_code | code object containing | |
| | | compiled function | |
| | | :term:`bytecode` | |
2007-08-15 11:28:01 -03:00
+-----------+-----------------+---------------------------+-------+
| | func_defaults | tuple of any default | |
| | | values for arguments | |
+-----------+-----------------+---------------------------+-------+
| | func_doc | (same as __doc__) | |
+-----------+-----------------+---------------------------+-------+
| | func_globals | global namespace in which | |
| | | this function was defined | |
+-----------+-----------------+---------------------------+-------+
| | func_name | (same as __name__) | |
+-----------+-----------------+---------------------------+-------+
| generator | __iter__ | defined to support | |
| | | iteration over container | |
+-----------+-----------------+---------------------------+-------+
| | close | raises new GeneratorExit | |
| | | exception inside the | |
| | | generator to terminate | |
| | | the iteration | |
+-----------+-----------------+---------------------------+-------+
| | gi_code | code object | |
+-----------+-----------------+---------------------------+-------+
| | gi_frame | frame object or possibly | |
| | | None once the generator | |
| | | has been exhausted | |
+-----------+-----------------+---------------------------+-------+
| | gi_running | set to 1 when generator | |
| | | is executing, 0 otherwise | |
+-----------+-----------------+---------------------------+-------+
| | next | return the next item from | |
| | | the container | |
+-----------+-----------------+---------------------------+-------+
| | send | resumes the generator and | |
| | | "sends" a value that | |
| | | becomes the result of the | |
| | | current yield-expression | |
+-----------+-----------------+---------------------------+-------+
| | throw | used to raise an | |
| | | exception inside the | |
| | | generator | |
+-----------+-----------------+---------------------------+-------+
2007-08-15 11:28:01 -03:00
| 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 | built-in namespace seen | |
| | | by this frame | |
+-----------+-----------------+---------------------------+-------+
| | f_code | code object being | |
| | | executed in this frame | |
+-----------+-----------------+---------------------------+-------+
| | f_exc_traceback | traceback if raised in | |
| | | this frame, or ``None`` | |
+-----------+-----------------+---------------------------+-------+
| | f_exc_type | exception type if raised | |
| | | in this frame, or | |
| | | ``None`` | |
+-----------+-----------------+---------------------------+-------+
| | f_exc_value | exception value if raised | |
| | | in this frame, or | |
| | | ``None`` | |
+-----------+-----------------+---------------------------+-------+
| | 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_restricted | 0 or 1 if frame is in | |
| | | restricted execution mode | |
+-----------+-----------------+---------------------------+-------+
| | f_trace | tracing function for this | |
| | | frame, or ``None`` | |
+-----------+-----------------+---------------------------+-------+
| code | co_argcount | number of arguments (not | |
| | | including \* or \*\* | |
| | | args) | |
+-----------+-----------------+---------------------------+-------+
| | co_code | string of raw compiled | |
| | | bytecode | |
+-----------+-----------------+---------------------------+-------+
| | 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: 1=optimized ``|`` | |
| | | 2=newlocals ``|`` 4=\*arg | |
| | | ``|`` 8=\*\*arg | |
+-----------+-----------------+---------------------------+-------+
| | co_lnotab | encoded mapping of line | |
| | | numbers to bytecode | |
| | | indices | |
+-----------+-----------------+---------------------------+-------+
| | co_name | name with which this code | |
| | | object was defined | |
+-----------+-----------------+---------------------------+-------+
| | co_names | tuple of names of local | |
| | | variables | |
+-----------+-----------------+---------------------------+-------+
| | co_nlocals | number of local variables | |
+-----------+-----------------+---------------------------+-------+
| | co_stacksize | virtual machine stack | |
| | | space required | |
+-----------+-----------------+---------------------------+-------+
| | co_varnames | tuple of names of | |
| | | arguments and local | |
| | | variables | |
+-----------+-----------------+---------------------------+-------+
| builtin | __doc__ | documentation string | |
+-----------+-----------------+---------------------------+-------+
| | __name__ | original name of this | |
| | | function or method | |
+-----------+-----------------+---------------------------+-------+
| | __self__ | instance to which a | |
| | | method is bound, or | |
| | | ``None`` | |
+-----------+-----------------+---------------------------+-------+
Note:
(1)
.. versionchanged:: 2.2
:attr:`im_class` used to refer to the class that defined the method.
.. function:: 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 is supplied, only members for which
the predicate returns a true value are included.
.. note::
:func:`getmembers` does not return metaclass attributes when the argument
is a class (this behavior is inherited from the :func:`dir` function).
2007-08-15 11:28:01 -03:00
.. function:: getmoduleinfo(path)
Return a tuple of values that describe how Python will interpret the file
identified by *path* if it is a module, or ``None`` if it would not be
identified as a module. The return tuple is ``(name, suffix, mode, mtype)``,
where *name* is the name of the module without the name of any enclosing
package, *suffix* is the trailing part of the file name (which may not be a
dot-delimited extension), *mode* is the :func:`open` mode that would be used
(``'r'`` or ``'rb'``), and *mtype* is an integer giving the type of the
module. *mtype* will have a value which can be compared to the constants
defined in the :mod:`imp` module; see the documentation for that module for
more information on module types.
2008-01-11 05:55:53 -04:00
.. versionchanged:: 2.6
Returns a :term:`named tuple` ``ModuleInfo(name, suffix, mode,
module_type)``.
2007-08-15 11:28:01 -03:00
.. function:: getmodulename(path)
Return the name of the module named by the file *path*, without including the
names of enclosing packages. This uses the same algorithm as the interpreter
uses when searching for modules. If the name cannot be matched according to the
interpreter's rules, ``None`` is returned.
.. function:: ismodule(object)
Return true if the object is a module.
.. function:: isclass(object)
Return true if the object is a class.
.. function:: ismethod(object)
Return true if the object is a method.
.. function:: isfunction(object)
Return true if the object is a Python function or unnamed (:term:`lambda`) function.
2007-08-15 11:28:01 -03:00
.. function:: isgeneratorfunction(object)
Return true if the object is a Python generator function.
2008-03-03 21:49:37 -04:00
.. versionadded:: 2.6
.. function:: isgenerator(object)
Return true if the object is a generator.
2007-08-15 11:28:01 -03:00
2008-03-03 21:49:37 -04:00
.. versionadded:: 2.6
2007-08-15 11:28:01 -03:00
.. function:: istraceback(object)
Return true if the object is a traceback.
.. function:: isframe(object)
Return true if the object is a frame.
.. function:: iscode(object)
Return true if the object is a code.
.. function:: isbuiltin(object)
Return true if the object is a built-in function.
.. function:: isroutine(object)
Return true if the object is a user-defined or built-in function or method.
.. function:: isabstract(object)
Return true if the object is an abstract base class.
.. versionadded:: 2.6
2007-08-15 11:28:01 -03:00
.. function:: ismethoddescriptor(object)
Return true if the object is a method descriptor, but not if :func:`ismethod`
or :func:`isclass` or :func:`isfunction` are true.
2007-08-15 11:28:01 -03:00
This is new as of Python 2.2, and, for example, is true of
``int.__add__``. An object passing this test has a :attr:`__get__` attribute
but not a :attr:`__set__` attribute, but beyond that the set of attributes
varies. :attr:`__name__` is usually sensible, and :attr:`__doc__` often is.
2007-08-15 11:28:01 -03:00
Methods implemented via descriptors that also pass one of the other tests
return false from the :func:`ismethoddescriptor` test, simply because the
other tests promise more -- you can, e.g., count on having the
:attr:`im_func` attribute (etc) when an object passes :func:`ismethod`.
2007-08-15 11:28:01 -03:00
.. function:: isdatadescriptor(object)
Return true if the object is a data descriptor.
Data descriptors have both a :attr:`__get__` and a :attr:`__set__` attribute.
Examples are properties (defined in Python), getsets, and members. The
latter two are defined in C and there are more specific tests available for
those types, which is robust across Python implementations. Typically, data
descriptors will also have :attr:`__name__` and :attr:`__doc__` attributes
(properties, getsets, and members have both of these attributes), but this is
not guaranteed.
2007-08-15 11:28:01 -03:00
.. versionadded:: 2.3
.. function:: isgetsetdescriptor(object)
Return true if the object is a getset descriptor.
Merged revisions 75363,75365,75376,75392,75394,75403,75418,75484,75572,75580,75590,75592,75594-75596,75600,75602-75603,75605-75607,75610-75613,75616-75617,75623,75627,75647 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r75363 | georg.brandl | 2009-10-11 20:31:23 +0200 (So, 11 Okt 2009) | 1 line Add the Python FAQ lists to the documentation. Copied from sandbox/faq. Many thanks to AMK for the preparation work. ........ r75365 | georg.brandl | 2009-10-11 22:16:16 +0200 (So, 11 Okt 2009) | 1 line Fix broken links found by "make linkcheck". scipy.org seems to be done right now, so I could not verify links going there. ........ r75376 | benjamin.peterson | 2009-10-12 03:26:07 +0200 (Mo, 12 Okt 2009) | 1 line platform we don't care about ........ r75392 | andrew.kuchling | 2009-10-13 18:11:49 +0200 (Di, 13 Okt 2009) | 1 line Various link, textual, and markup fixes ........ r75394 | georg.brandl | 2009-10-13 20:10:59 +0200 (Di, 13 Okt 2009) | 1 line Fix markup. ........ r75403 | georg.brandl | 2009-10-14 17:57:46 +0200 (Mi, 14 Okt 2009) | 1 line #7126: os.environ changes *do* take effect in subprocesses started with os.system(). ........ r75418 | georg.brandl | 2009-10-14 20:48:32 +0200 (Mi, 14 Okt 2009) | 1 line #7116: str.join() takes an iterable. ........ r75484 | georg.brandl | 2009-10-18 09:58:12 +0200 (So, 18 Okt 2009) | 1 line Fix missing word. ........ r75572 | benjamin.peterson | 2009-10-20 23:55:17 +0200 (Di, 20 Okt 2009) | 1 line clarify buffer arg #7178 ........ r75580 | georg.brandl | 2009-10-21 09:15:59 +0200 (Mi, 21 Okt 2009) | 1 line #7170: fix explanation about non-weakrefable builtin types. ........ r75590 | benjamin.peterson | 2009-10-22 04:36:47 +0200 (Do, 22 Okt 2009) | 1 line rewrite to be nice to other implementations ........ r75592 | georg.brandl | 2009-10-22 09:05:48 +0200 (Do, 22 Okt 2009) | 1 line Fix punctuation. ........ r75594 | georg.brandl | 2009-10-22 09:56:02 +0200 (Do, 22 Okt 2009) | 1 line Fix markup. ........ r75595 | georg.brandl | 2009-10-22 09:56:56 +0200 (Do, 22 Okt 2009) | 1 line Fix duplicate target. ........ r75596 | georg.brandl | 2009-10-22 10:05:04 +0200 (Do, 22 Okt 2009) | 1 line Add a new directive marking up implementation details and start using it. ........ r75600 | georg.brandl | 2009-10-22 13:01:46 +0200 (Do, 22 Okt 2009) | 1 line Make it more robust. ........ r75602 | georg.brandl | 2009-10-22 13:28:06 +0200 (Do, 22 Okt 2009) | 1 line Document new directive. ........ r75603 | georg.brandl | 2009-10-22 13:28:23 +0200 (Do, 22 Okt 2009) | 1 line Allow short form with text as argument. ........ r75605 | georg.brandl | 2009-10-22 13:48:10 +0200 (Do, 22 Okt 2009) | 1 line Use "impl-detail" directive where applicable. ........ r75606 | georg.brandl | 2009-10-22 17:00:06 +0200 (Do, 22 Okt 2009) | 1 line #6324: membership test tries iteration via __iter__. ........ r75607 | georg.brandl | 2009-10-22 17:04:09 +0200 (Do, 22 Okt 2009) | 1 line #7088: document new functions in signal as Unix-only. ........ r75610 | georg.brandl | 2009-10-22 17:27:24 +0200 (Do, 22 Okt 2009) | 1 line Reorder __slots__ fine print and add a clarification. ........ r75611 | georg.brandl | 2009-10-22 17:42:32 +0200 (Do, 22 Okt 2009) | 1 line #7035: improve docs of the various <method>_errors() functions, and give them docstrings. ........ r75612 | georg.brandl | 2009-10-22 17:52:15 +0200 (Do, 22 Okt 2009) | 1 line #7156: document curses as Unix-only. ........ r75613 | georg.brandl | 2009-10-22 17:54:35 +0200 (Do, 22 Okt 2009) | 1 line #6977: getopt does not support optional option arguments. ........ r75616 | georg.brandl | 2009-10-22 18:17:05 +0200 (Do, 22 Okt 2009) | 1 line Add proper references. ........ r75617 | georg.brandl | 2009-10-22 18:20:55 +0200 (Do, 22 Okt 2009) | 1 line Make printout margin important. ........ r75623 | georg.brandl | 2009-10-23 10:14:44 +0200 (Fr, 23 Okt 2009) | 1 line #7188: fix optionxform() docs. ........ r75627 | fred.drake | 2009-10-23 15:04:51 +0200 (Fr, 23 Okt 2009) | 2 lines add further note about what's passed to optionxform ........ r75647 | georg.brandl | 2009-10-24 12:04:19 +0200 (Sa, 24 Okt 2009) | 1 line Fix markup. ........
2009-10-27 12:08:27 -03:00
.. impl-detail::
getsets are attributes defined in extension modules via
:ctype:`PyGetSetDef` structures. For Python implementations without such
types, this method will always return ``False``.
2007-08-15 11:28:01 -03:00
.. versionadded:: 2.5
.. function:: ismemberdescriptor(object)
Return true if the object is a member descriptor.
Merged revisions 75363,75365,75376,75392,75394,75403,75418,75484,75572,75580,75590,75592,75594-75596,75600,75602-75603,75605-75607,75610-75613,75616-75617,75623,75627,75647 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r75363 | georg.brandl | 2009-10-11 20:31:23 +0200 (So, 11 Okt 2009) | 1 line Add the Python FAQ lists to the documentation. Copied from sandbox/faq. Many thanks to AMK for the preparation work. ........ r75365 | georg.brandl | 2009-10-11 22:16:16 +0200 (So, 11 Okt 2009) | 1 line Fix broken links found by "make linkcheck". scipy.org seems to be done right now, so I could not verify links going there. ........ r75376 | benjamin.peterson | 2009-10-12 03:26:07 +0200 (Mo, 12 Okt 2009) | 1 line platform we don't care about ........ r75392 | andrew.kuchling | 2009-10-13 18:11:49 +0200 (Di, 13 Okt 2009) | 1 line Various link, textual, and markup fixes ........ r75394 | georg.brandl | 2009-10-13 20:10:59 +0200 (Di, 13 Okt 2009) | 1 line Fix markup. ........ r75403 | georg.brandl | 2009-10-14 17:57:46 +0200 (Mi, 14 Okt 2009) | 1 line #7126: os.environ changes *do* take effect in subprocesses started with os.system(). ........ r75418 | georg.brandl | 2009-10-14 20:48:32 +0200 (Mi, 14 Okt 2009) | 1 line #7116: str.join() takes an iterable. ........ r75484 | georg.brandl | 2009-10-18 09:58:12 +0200 (So, 18 Okt 2009) | 1 line Fix missing word. ........ r75572 | benjamin.peterson | 2009-10-20 23:55:17 +0200 (Di, 20 Okt 2009) | 1 line clarify buffer arg #7178 ........ r75580 | georg.brandl | 2009-10-21 09:15:59 +0200 (Mi, 21 Okt 2009) | 1 line #7170: fix explanation about non-weakrefable builtin types. ........ r75590 | benjamin.peterson | 2009-10-22 04:36:47 +0200 (Do, 22 Okt 2009) | 1 line rewrite to be nice to other implementations ........ r75592 | georg.brandl | 2009-10-22 09:05:48 +0200 (Do, 22 Okt 2009) | 1 line Fix punctuation. ........ r75594 | georg.brandl | 2009-10-22 09:56:02 +0200 (Do, 22 Okt 2009) | 1 line Fix markup. ........ r75595 | georg.brandl | 2009-10-22 09:56:56 +0200 (Do, 22 Okt 2009) | 1 line Fix duplicate target. ........ r75596 | georg.brandl | 2009-10-22 10:05:04 +0200 (Do, 22 Okt 2009) | 1 line Add a new directive marking up implementation details and start using it. ........ r75600 | georg.brandl | 2009-10-22 13:01:46 +0200 (Do, 22 Okt 2009) | 1 line Make it more robust. ........ r75602 | georg.brandl | 2009-10-22 13:28:06 +0200 (Do, 22 Okt 2009) | 1 line Document new directive. ........ r75603 | georg.brandl | 2009-10-22 13:28:23 +0200 (Do, 22 Okt 2009) | 1 line Allow short form with text as argument. ........ r75605 | georg.brandl | 2009-10-22 13:48:10 +0200 (Do, 22 Okt 2009) | 1 line Use "impl-detail" directive where applicable. ........ r75606 | georg.brandl | 2009-10-22 17:00:06 +0200 (Do, 22 Okt 2009) | 1 line #6324: membership test tries iteration via __iter__. ........ r75607 | georg.brandl | 2009-10-22 17:04:09 +0200 (Do, 22 Okt 2009) | 1 line #7088: document new functions in signal as Unix-only. ........ r75610 | georg.brandl | 2009-10-22 17:27:24 +0200 (Do, 22 Okt 2009) | 1 line Reorder __slots__ fine print and add a clarification. ........ r75611 | georg.brandl | 2009-10-22 17:42:32 +0200 (Do, 22 Okt 2009) | 1 line #7035: improve docs of the various <method>_errors() functions, and give them docstrings. ........ r75612 | georg.brandl | 2009-10-22 17:52:15 +0200 (Do, 22 Okt 2009) | 1 line #7156: document curses as Unix-only. ........ r75613 | georg.brandl | 2009-10-22 17:54:35 +0200 (Do, 22 Okt 2009) | 1 line #6977: getopt does not support optional option arguments. ........ r75616 | georg.brandl | 2009-10-22 18:17:05 +0200 (Do, 22 Okt 2009) | 1 line Add proper references. ........ r75617 | georg.brandl | 2009-10-22 18:20:55 +0200 (Do, 22 Okt 2009) | 1 line Make printout margin important. ........ r75623 | georg.brandl | 2009-10-23 10:14:44 +0200 (Fr, 23 Okt 2009) | 1 line #7188: fix optionxform() docs. ........ r75627 | fred.drake | 2009-10-23 15:04:51 +0200 (Fr, 23 Okt 2009) | 2 lines add further note about what's passed to optionxform ........ r75647 | georg.brandl | 2009-10-24 12:04:19 +0200 (Sa, 24 Okt 2009) | 1 line Fix markup. ........
2009-10-27 12:08:27 -03:00
.. impl-detail::
Member descriptors are attributes defined in extension modules via
:ctype:`PyMemberDef` structures. For Python implementations without such
types, this method will always return ``False``.
2007-08-15 11:28:01 -03:00
.. versionadded:: 2.5
.. _inspect-source:
Retrieving source code
----------------------
.. function:: getdoc(object)
Get the documentation string for an object, cleaned up with :func:`cleandoc`.
2007-08-15 11:28:01 -03:00
.. function:: getcomments(object)
Return in a single string any lines of comments immediately preceding the
object's source code (for a class, function, or method), or at the top of the
Python source file (if the object is a module).
.. function:: getfile(object)
Return the name of the (text or binary) file in which an object was defined.
This will fail with a :exc:`TypeError` if the object is a built-in module,
class, or function.
.. function:: getmodule(object)
Try to guess which module an object was defined in.
.. function:: getsourcefile(object)
Return the name of the Python source file in which an object was defined. This
will fail with a :exc:`TypeError` if the object is a built-in module, class, or
function.
.. function:: getsourcelines(object)
Return a list of source lines and starting line number for an object. The
argument may be a module, class, method, function, traceback, frame, or code
object. The source code is returned as a list of the lines corresponding to the
object and the line number indicates where in the original source file the first
line of code was found. An :exc:`IOError` is raised if the source code cannot
be retrieved.
.. function:: getsource(object)
Return the text of the source code for an object. The argument may be a module,
class, method, function, traceback, frame, or code object. The source code is
returned as a single string. An :exc:`IOError` is raised if the source code
cannot be retrieved.
.. function:: cleandoc(doc)
Clean up indentation from docstrings that are indented to line up with blocks
of code. Any whitespace that can be uniformly removed from the second line
onwards is removed. Also, all tabs are expanded to spaces.
.. versionadded:: 2.6
2007-08-15 11:28:01 -03:00
.. _inspect-classes-functions:
Classes and functions
---------------------
.. function:: getclasstree(classes[, unique])
Arrange the given list of classes into a hierarchy of nested lists. Where a
nested list appears, it contains classes derived from the class whose entry
immediately precedes the list. Each entry is a 2-tuple containing a class and a
tuple of its base classes. If the *unique* argument is true, exactly one entry
appears in the returned structure for each class in the given list. Otherwise,
classes using multiple inheritance and their descendants will appear multiple
times.
.. function:: getargspec(func)
Get the names and default values of a function's arguments. A tuple of four
things is returned: ``(args, varargs, varkw, defaults)``. *args* is a list of
the argument names (it may contain nested lists). *varargs* and *varkw* are the
names of the ``*`` and ``**`` arguments or ``None``. *defaults* is a tuple of
default argument values or None if there are no default arguments; if this tuple
has *n* elements, they correspond to the last *n* elements listed in *args*.
2008-01-11 05:55:53 -04:00
.. versionchanged:: 2.6
Returns a :term:`named tuple` ``ArgSpec(args, varargs, keywords,
defaults)``.
2007-08-15 11:28:01 -03:00
.. function:: getargvalues(frame)
Get information about arguments passed into a particular frame. A tuple of four
things is returned: ``(args, varargs, varkw, locals)``. *args* is a list of the
argument names (it may contain nested lists). *varargs* and *varkw* are the
names of the ``*`` and ``**`` arguments or ``None``. *locals* is the locals
dictionary of the given frame.
2008-01-11 05:55:53 -04:00
.. versionchanged:: 2.6
Returns a :term:`named tuple` ``ArgInfo(args, varargs, keywords,
locals)``.
2007-08-15 11:28:01 -03:00
.. function:: formatargspec(args[, varargs, varkw, defaults, formatarg, formatvarargs, formatvarkw, formatvalue, join])
Format a pretty argument spec from the four values returned by
:func:`getargspec`. The format\* arguments are the corresponding optional
formatting functions that are called to turn names and values into strings.
.. function:: formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue, join])
Format a pretty argument spec from the four values returned by
:func:`getargvalues`. The format\* arguments are the corresponding optional
formatting functions that are called to turn names and values into strings.
.. function:: getmro(cls)
Return a tuple of class cls's base classes, including cls, in method resolution
order. No class appears more than once in this tuple. Note that the method
resolution order depends on cls's type. Unless a very peculiar user-defined
metatype is in use, cls will be the first element of the tuple.
.. _inspect-stack:
The interpreter stack
---------------------
When the following functions return "frame records," each record is a tuple of
six items: the frame object, the filename, the line number of the current line,
the function name, a list of lines of context from the source code, and the
index of the current line within that list.
.. note::
2007-08-15 11:28:01 -03:00
Keeping references to frame objects, as found in the first element of the frame
records these functions return, can cause your program to create reference
cycles. Once a reference cycle has been created, the lifespan of all objects
which can be accessed from the objects which form the cycle can become much
longer even if Python's optional cycle detector is enabled. If such cycles must
be created, it is important to ensure they are explicitly broken to avoid the
delayed destruction of objects and increased memory consumption which occurs.
Though the cycle detector will catch these, destruction of the frames (and local
variables) can be made deterministic by removing the cycle in a
:keyword:`finally` clause. This is also important if the cycle detector was
disabled when Python was compiled or using :func:`gc.disable`. For example::
def handle_stackframe_without_leak():
frame = inspect.currentframe()
try:
# do something with the frame
finally:
del frame
The optional *context* argument supported by most of these functions specifies
the number of lines of context to return, which are centered around the current
line.
.. function:: getframeinfo(frame[, context])
Get information about a frame or traceback object. A 5-tuple is returned, the
last five elements of the frame's frame record.
2008-01-11 05:55:53 -04:00
.. versionchanged:: 2.6
Returns a :term:`named tuple` ``Traceback(filename, lineno, function,
code_context, index)``.
2007-08-15 11:28:01 -03:00
.. function:: getouterframes(frame[, context])
Get a list of frame records for a frame and all outer frames. These frames
represent the calls that lead to the creation of *frame*. The first entry in the
returned list represents *frame*; the last entry represents the outermost call
on *frame*'s stack.
.. function:: getinnerframes(traceback[, context])
Get a list of frame records for a traceback's frame and all inner frames. These
frames represent calls made as a consequence of *frame*. The first entry in the
list represents *traceback*; the last entry represents where the exception was
raised.
.. function:: currentframe()
Return the frame object for the caller's stack frame.
Merged revisions 75363,75365,75376,75392,75394,75403,75418,75484,75572,75580,75590,75592,75594-75596,75600,75602-75603,75605-75607,75610-75613,75616-75617,75623,75627,75647 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r75363 | georg.brandl | 2009-10-11 20:31:23 +0200 (So, 11 Okt 2009) | 1 line Add the Python FAQ lists to the documentation. Copied from sandbox/faq. Many thanks to AMK for the preparation work. ........ r75365 | georg.brandl | 2009-10-11 22:16:16 +0200 (So, 11 Okt 2009) | 1 line Fix broken links found by "make linkcheck". scipy.org seems to be done right now, so I could not verify links going there. ........ r75376 | benjamin.peterson | 2009-10-12 03:26:07 +0200 (Mo, 12 Okt 2009) | 1 line platform we don't care about ........ r75392 | andrew.kuchling | 2009-10-13 18:11:49 +0200 (Di, 13 Okt 2009) | 1 line Various link, textual, and markup fixes ........ r75394 | georg.brandl | 2009-10-13 20:10:59 +0200 (Di, 13 Okt 2009) | 1 line Fix markup. ........ r75403 | georg.brandl | 2009-10-14 17:57:46 +0200 (Mi, 14 Okt 2009) | 1 line #7126: os.environ changes *do* take effect in subprocesses started with os.system(). ........ r75418 | georg.brandl | 2009-10-14 20:48:32 +0200 (Mi, 14 Okt 2009) | 1 line #7116: str.join() takes an iterable. ........ r75484 | georg.brandl | 2009-10-18 09:58:12 +0200 (So, 18 Okt 2009) | 1 line Fix missing word. ........ r75572 | benjamin.peterson | 2009-10-20 23:55:17 +0200 (Di, 20 Okt 2009) | 1 line clarify buffer arg #7178 ........ r75580 | georg.brandl | 2009-10-21 09:15:59 +0200 (Mi, 21 Okt 2009) | 1 line #7170: fix explanation about non-weakrefable builtin types. ........ r75590 | benjamin.peterson | 2009-10-22 04:36:47 +0200 (Do, 22 Okt 2009) | 1 line rewrite to be nice to other implementations ........ r75592 | georg.brandl | 2009-10-22 09:05:48 +0200 (Do, 22 Okt 2009) | 1 line Fix punctuation. ........ r75594 | georg.brandl | 2009-10-22 09:56:02 +0200 (Do, 22 Okt 2009) | 1 line Fix markup. ........ r75595 | georg.brandl | 2009-10-22 09:56:56 +0200 (Do, 22 Okt 2009) | 1 line Fix duplicate target. ........ r75596 | georg.brandl | 2009-10-22 10:05:04 +0200 (Do, 22 Okt 2009) | 1 line Add a new directive marking up implementation details and start using it. ........ r75600 | georg.brandl | 2009-10-22 13:01:46 +0200 (Do, 22 Okt 2009) | 1 line Make it more robust. ........ r75602 | georg.brandl | 2009-10-22 13:28:06 +0200 (Do, 22 Okt 2009) | 1 line Document new directive. ........ r75603 | georg.brandl | 2009-10-22 13:28:23 +0200 (Do, 22 Okt 2009) | 1 line Allow short form with text as argument. ........ r75605 | georg.brandl | 2009-10-22 13:48:10 +0200 (Do, 22 Okt 2009) | 1 line Use "impl-detail" directive where applicable. ........ r75606 | georg.brandl | 2009-10-22 17:00:06 +0200 (Do, 22 Okt 2009) | 1 line #6324: membership test tries iteration via __iter__. ........ r75607 | georg.brandl | 2009-10-22 17:04:09 +0200 (Do, 22 Okt 2009) | 1 line #7088: document new functions in signal as Unix-only. ........ r75610 | georg.brandl | 2009-10-22 17:27:24 +0200 (Do, 22 Okt 2009) | 1 line Reorder __slots__ fine print and add a clarification. ........ r75611 | georg.brandl | 2009-10-22 17:42:32 +0200 (Do, 22 Okt 2009) | 1 line #7035: improve docs of the various <method>_errors() functions, and give them docstrings. ........ r75612 | georg.brandl | 2009-10-22 17:52:15 +0200 (Do, 22 Okt 2009) | 1 line #7156: document curses as Unix-only. ........ r75613 | georg.brandl | 2009-10-22 17:54:35 +0200 (Do, 22 Okt 2009) | 1 line #6977: getopt does not support optional option arguments. ........ r75616 | georg.brandl | 2009-10-22 18:17:05 +0200 (Do, 22 Okt 2009) | 1 line Add proper references. ........ r75617 | georg.brandl | 2009-10-22 18:20:55 +0200 (Do, 22 Okt 2009) | 1 line Make printout margin important. ........ r75623 | georg.brandl | 2009-10-23 10:14:44 +0200 (Fr, 23 Okt 2009) | 1 line #7188: fix optionxform() docs. ........ r75627 | fred.drake | 2009-10-23 15:04:51 +0200 (Fr, 23 Okt 2009) | 2 lines add further note about what's passed to optionxform ........ r75647 | georg.brandl | 2009-10-24 12:04:19 +0200 (Sa, 24 Okt 2009) | 1 line Fix markup. ........
2009-10-27 12:08:27 -03:00
.. impl-detail::
This function relies on Python stack frame support in the interpreter,
which isn't guaranteed to exist in all implementations of Python. If
running in an implementation without Python stack frame support this
function returns ``None``.
Merged revisions 74492,74531,74545-74550,74553-74555,74588,74603,74608,74614,74616-74618,74631-74633,74652-74653,74666,74671,74737,74739,74779,74781-74782,74784,74791,74793,74818-74820,74822,74832 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74492 | r.david.murray | 2009-08-17 21:26:49 +0200 (Mo, 17 Aug 2009) | 2 lines Issue 6685: 'toupper' -> 'upper' in cgi doc example explanation. ........ r74531 | vinay.sajip | 2009-08-21 00:04:32 +0200 (Fr, 21 Aug 2009) | 1 line Added section on exceptions raised during logging. ........ r74545 | georg.brandl | 2009-08-24 19:14:29 +0200 (Mo, 24 Aug 2009) | 1 line #6772: mention utf-8 as utf8 alias. ........ r74546 | georg.brandl | 2009-08-24 19:20:40 +0200 (Mo, 24 Aug 2009) | 1 line #6725: spell "namespace" consistently. ........ r74547 | georg.brandl | 2009-08-24 19:22:05 +0200 (Mo, 24 Aug 2009) | 1 line #6718: fix example. ........ r74548 | georg.brandl | 2009-08-24 19:24:27 +0200 (Mo, 24 Aug 2009) | 1 line #6677: mention "deleting" as an alias for removing files. ........ r74549 | benjamin.peterson | 2009-08-24 19:42:36 +0200 (Mo, 24 Aug 2009) | 1 line fix pdf building by teaching latex the right encoding package ........ r74550 | georg.brandl | 2009-08-24 19:48:40 +0200 (Mo, 24 Aug 2009) | 1 line #6677: note that rmdir only removes empty directories. ........ r74553 | r.david.murray | 2009-08-27 03:04:59 +0200 (Do, 27 Aug 2009) | 2 lines Remove leftover text from end of sentence. ........ r74554 | georg.brandl | 2009-08-27 20:59:02 +0200 (Do, 27 Aug 2009) | 1 line Typo fix. ........ r74555 | georg.brandl | 2009-08-27 21:02:43 +0200 (Do, 27 Aug 2009) | 1 line #6787: reference fix. ........ r74588 | georg.brandl | 2009-08-30 10:35:01 +0200 (So, 30 Aug 2009) | 1 line #6803: fix old name. ........ r74603 | georg.brandl | 2009-08-31 08:38:29 +0200 (Mo, 31 Aug 2009) | 1 line other -> others where multiple arguments are accepted. ........ r74608 | senthil.kumaran | 2009-08-31 18:40:27 +0200 (Mo, 31 Aug 2009) | 3 lines Doc fix for the issue2637. ........ r74614 | georg.brandl | 2009-09-01 09:40:54 +0200 (Di, 01 Sep 2009) | 1 line #6813: better documentation for numberless string formats. ........ r74616 | georg.brandl | 2009-09-01 09:46:26 +0200 (Di, 01 Sep 2009) | 1 line #6808: clarification. ........ r74617 | georg.brandl | 2009-09-01 09:53:37 +0200 (Di, 01 Sep 2009) | 1 line #6765: hint that log(x, base) is not very sophisticated. ........ r74618 | georg.brandl | 2009-09-01 10:00:47 +0200 (Di, 01 Sep 2009) | 1 line #6810: add a link to the section about frame objects instead of just a description where to find it. ........ r74631 | georg.brandl | 2009-09-02 22:37:16 +0200 (Mi, 02 Sep 2009) | 1 line #6821: fix signature of PyBuffer_Release(). ........ r74632 | georg.brandl | 2009-09-03 09:27:26 +0200 (Do, 03 Sep 2009) | 1 line #6828: fix wrongly highlighted blocks. ........ r74633 | georg.brandl | 2009-09-03 14:31:39 +0200 (Do, 03 Sep 2009) | 1 line #6757: complete the list of types that marshal can serialize. ........ r74652 | georg.brandl | 2009-09-04 13:25:37 +0200 (Fr, 04 Sep 2009) | 1 line #6756: add some info about the "acct" parameter. ........ r74653 | georg.brandl | 2009-09-04 13:32:18 +0200 (Fr, 04 Sep 2009) | 1 line #6777: dont discourage usage of Exception.args or promote usage of Exception.message. ........ r74666 | georg.brandl | 2009-09-05 11:04:09 +0200 (Sa, 05 Sep 2009) | 1 line #6841: remove duplicated word. ........ r74671 | georg.brandl | 2009-09-05 18:47:17 +0200 (Sa, 05 Sep 2009) | 1 line #6843: add link from filterwarnings to where the meaning of the arguments is covered. ........ r74737 | georg.brandl | 2009-09-09 18:49:13 +0200 (Mi, 09 Sep 2009) | 1 line Properly document copy and deepcopy as functions. ........ r74739 | georg.brandl | 2009-09-11 09:55:20 +0200 (Fr, 11 Sep 2009) | 1 line Move function back to its section. ........ r74779 | michael.foord | 2009-09-13 18:13:36 +0200 (So, 13 Sep 2009) | 1 line Change to tutorial wording for reading text / binary files on Windows. Issue #6301. ........ r74781 | michael.foord | 2009-09-13 18:46:19 +0200 (So, 13 Sep 2009) | 1 line Note that sys._getframe is not guaranteed to exist in all implementations of Python, and a corresponding note in inspect.currentframe. Issue 6712. ........ r74782 | michael.foord | 2009-09-13 19:07:46 +0200 (So, 13 Sep 2009) | 1 line Tutorial tweaks. Issue 6849. ........ r74784 | georg.brandl | 2009-09-13 20:15:07 +0200 (So, 13 Sep 2009) | 1 line Typo fix. ........ r74791 | georg.brandl | 2009-09-14 16:08:54 +0200 (Mo, 14 Sep 2009) | 1 line #6574: list the future features in a table. ........ r74793 | georg.brandl | 2009-09-14 16:50:47 +0200 (Mo, 14 Sep 2009) | 1 line #6908: fix association of hashlib hash attributes. ........ r74818 | georg.brandl | 2009-09-16 11:23:04 +0200 (Mi, 16 Sep 2009) | 1 line #6880: add reference to classes section in exceptions section, which comes earlier. ........ r74819 | georg.brandl | 2009-09-16 11:24:57 +0200 (Mi, 16 Sep 2009) | 1 line #6876: fix base class constructor invocation in example. ........ r74820 | georg.brandl | 2009-09-16 11:30:48 +0200 (Mi, 16 Sep 2009) | 1 line #6891: comment out dead link to Unicode article. ........ r74822 | georg.brandl | 2009-09-16 12:12:06 +0200 (Mi, 16 Sep 2009) | 1 line #5621: refactor description of how class/instance attributes interact on a.x=a.x+1 or augassign. ........ r74832 | georg.brandl | 2009-09-16 17:57:46 +0200 (Mi, 16 Sep 2009) | 1 line Rewrap long lines. ........
2009-10-27 11:50:20 -03:00
2007-08-15 11:28:01 -03:00
.. function:: stack([context])
Return a list of frame records for the caller's stack. The first entry in the
returned list represents the caller; the last entry represents the outermost
call on the stack.
.. function:: trace([context])
Return a list of frame records for the stack between the current frame and the
frame in which an exception currently being handled was raised in. The first
entry in the list represents the caller; the last entry represents where the
exception was raised.