26.10.1 Types and members

The getmembers() function retrieves the members of an object such as a class or module. The eleven 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:

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 for this method (1)
im_func function object containing implementation of method
im_self instance to which this method is bound, or None
function __doc__ documentation string
__name__ name with which this function was defined
func_code code object containing compiled function bytecode
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__)
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)
Changed in version 2.2: im_class used to refer to the class that defined the method.

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.

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 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 imp module; see the documentation for that module for more information on module types.

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.

ismodule( object)
Return true if the object is a module.

isclass( object)
Return true if the object is a class.

ismethod( object)
Return true if the object is a method.

isfunction( object)
Return true if the object is a Python function or unnamed (lambda) function.

istraceback( object)
Return true if the object is a traceback.

isframe( object)
Return true if the object is a frame.

iscode( object)
Return true if the object is a code.

isbuiltin( object)
Return true if the object is a built-in function.

isroutine( object)
Return true if the object is a user-defined or built-in function or method.

ismethoddescriptor( object)
Return true if the object is a method descriptor, but not if ismethod() or isclass() or isfunction() are true.

This is new as of Python 2.2, and, for example, is true of int.__add__. An object passing this test has a __get__ attribute but not a __set__ attribute, but beyond that the set of attributes varies. __name__ is usually sensible, and __doc__ often is.

Methods implemented via descriptors that also pass one of the other tests return false from the ismethoddescriptor() test, simply because the other tests promise more - you can, e.g., count on having the im_func attribute (etc) when an object passes ismethod().

isdatadescriptor( object)
Return true if the object is a data descriptor.

Data descriptors have both a __get__ and a __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 __name__ and __doc__ attributes (properties, getsets, and members have both of these attributes), but this is not guaranteed. New in version 2.3.

isgetsetdescriptor( object)
Return true if the object is a getset descriptor.

getsets are attributes defined in extension modules via PyGetSetDef structures. For Python implementations without such types, this method will always return False. New in version 2.5.

ismemberdescriptor( object)
Return true if the object is a member descriptor.

Member descriptors are attributes defined in extension modules via PyMemberDef structures. For Python implementations without such types, this method will always return False. New in version 2.5.

See About this document... for information on suggesting changes.