Next: Class definitions
Up: Compound statements
Previous: The try statement
A function definition defines a user-defined function object (see
section ):
funcdef: "def" funcname "(" [parameter_list] ")" ":" suite
parameter_list: (defparameter ",")* ("*" identifier | defparameter [","])
defparameter: parameter ["=" condition]
sublist: parameter ("," parameter)* [","]
parameter: identifier | "(" sublist ")"
funcname: identifier
A function definition is an executable statement. Its execution binds
the function name in the current local name space to a function object
(a wrapper around the executable code for the function). This
function object contains a reference to the current global name space
as the global name space to be used when the function is called.
The function definition does not execute the function body; this gets
executed only when the function is called.
When one or more top-level parameters have the form parameter =
condition, the function is said to have ``default parameter values''.
Default parameter values are evaluated when the function definition is
executed. For a parameter with a default value, the correponding
argument may be omitted from a call, in which case the parameter's
default value is substituted. If a parameter has a default value, all
following parameters must also have a default value - this is a
syntactic restriction that is not expressed by the grammar.
Function call semantics are described in section . When a
user-defined function is called, first missing arguments for which a
default value exists are supplied; then the arguments (a.k.a. actual
parameters) are bound to the (formal) parameters, as follows:
-
If there are no formal parameters, there must be no arguments.
-
If the formal parameter list does not end in a star followed by an
identifier, there must be exactly as many arguments as there are
parameters in the formal parameter list (at the top level); the
arguments are assigned to the formal parameters one by one. Note that
the presence or absence of a trailing comma at the top level in either
the formal or the actual parameter list makes no difference. The
assignment to a formal parameter is performed as if the parameter
occurs on the left hand side of an assignment statement whose right
hand side's value is that of the argument.
-
If the formal parameter list ends in a star followed by an identifier,
preceded by zero or more comma-followed parameters, there must be at
least as many arguments as there are parameters preceding the star.
Call this number N. The first N arguments are assigned to
the corresponding formal parameters in the way descibed above. A
tuple containing the remaining arguments, if any, is then assigned to
the identifier following the star. This variable will always be a
tuple: if there are no extra arguments, its value is (), if
there is just one extra argument, it is a singleton tuple.
Note that the `variable length parameter list' feature only works at
the top level of the parameter list; individual parameters use a model
corresponding more closely to that of ordinary assignment. While the
latter model is generally preferable, because of the greater type
safety it offers (wrong-sized tuples aren't silently mistreated),
variable length parameter lists are a sufficiently accepted practice
in most programming languages that a compromise has been worked out.
(And anyway, assignment has no equivalent for empty argument lists.)
It is also possible to create anonymous functions (functions not bound
to a name), for immediate use in expressions. This uses lambda forms,
described in section .
Next: Class definitions
Up: Compound statements
Previous: The try statement