We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

Function Objects

A user-defined function object is created by a function definition (see def). 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 The function’s documentation string, or None if unavailable. (Writable.)
  • __doc__ Another way of spelling func_doc. (Writable.)
  • func_name The function’s name. (Writable.)
  • __name__ Another way of spelling func_name. (Writable.)
  • __module__ The name of the module the function was defined in, or None if unavailable. (Writable.)
  • func_defaults A tuple containing default argument values for those arguments that have defaults, or None if no arguments have a default value. If not None, the len(func_defaults) arguments have default values. (Writable.)
  • func_code The code object representing the compiled function body. (Writable.)
  • func_globals A reference to the dictionary that holds the function’s global variables — the global namespace of the module in which the function was defined. (Read-only.)
  • func_dict The namespace supporting arbitrary function attributes. (Writable.)
  • func_closure None or a tuple of cells that contain bindings for the function’s free variables. (Read-only.)

Most of the attributes labelled Writable check the type of the assigned value.

Changed in version 2.4: func_name is now writable.

Function objects also support getting and setting arbitrary attributes, which can be used, for example, to attach metadata to functions. Regular attribute dot-notation is used to get and set such attributes. Note that the current implementation only supports function attributes on user-defined functions. Function attributes on built-in functions may be supported in the future.

Additional information about a function’s definition can be retrieved from its code object (see type-code).