3.2 The standard type hierarchy (original) (raw)
A user-defined function object is created by a function definition (see section 7.5, ``Function definitions''). It should be called with an argument list containing the same number of items as the function's formal parameter list.
Special attributes: func_doc or __doc__ is the function's documentation string, or None if unavailable;func_name or __name__ is the function's name;func_defaults is a tuple containing default argument values for those arguments that have defaults, or None
if no arguments have a default value; func_code is the code object representing the compiled function body; func_globals is (a reference to) the dictionary that holds the function's global variables -- it defines the global namespace of the module in which the function was defined; func_dict or __dict__ contains the namespace supporting arbitrary function attributes;func_closure is None
or a tuple of cells that contain binding for the function's free variables.
Of these, func_code, func_defaults, func_closure,func_doc/__doc__, andfunc_dict/__dict__ may be writable; the others can never be changed. Additional information about a function's definition can be retrieved from its code object; see the description of internal types below.
In Python 2.1, the func_closure slot is always None
unless nested scopes are enabled. (See the appendix.)
A user-defined method object combines a class, a class instance (orNone
) and a user-defined function.
Special read-only attributes: im_self is the class instance object, im_func is the function object;im_class is the class that defined the method (which may be a base class of the class of which im_self is an instance);__doc__ is the method's documentation (same asim_func.__doc__
); __name__ is the method name (same asim_func.__name__
).
Methods also support accessing (but not setting) the arbitrary function attributes on the underlying function object.
User-defined method objects are created in two ways: when getting an attribute of a class that is a user-defined function object, or when getting an attribute of a class instance that is a user-defined function object defined by the class of the instance. In the former case (class attribute), the im_self attribute is None
, and the method object is said to be unbound; in the latter case (instance attribute), im_self is the instance, and the method object is said to be bound. For instance, when C is a class which contains a definition for a function f(), C.f
does not yield the function objectf
; rather, it yields an unbound method object m
wherem.im_class
is C, m.im_func
is f(), andm.im_self
is None
. When x
is a Cinstance, x.f
yields a bound method object m
wherem.im_class
is C
, m.im_func
is f(), andm.im_self
is x
.
When an unbound user-defined method object is called, the underlying function (im_func) is called, with the restriction that the first argument must be an instance of the proper class (im_class) or of a derived class thereof.
When a bound user-defined method object is called, the underlying function (im_func) is called, inserting the class instance (im_self) in front of the argument list. For instance, whenC is a class which contains a definition for a functionf(), and x
is an instance of C, callingx.f(1)
is equivalent to calling C.f(x, 1)
.
Note that the transformation from function object to (unbound or bound) method object happens each time the attribute is retrieved from the class or instance. In some cases, a fruitful optimization is to assign the attribute to a local variable and call that local variable. Also notice that this transformation only happens for user-defined functions; other callable objects (and all non-callable objects) are retrieved without transformation. It is also important to note that user-defined functions which are attributes of a class instance are not converted to bound methods; this only happens when the function is an attribute of the class.