The following are some simple user-defined functions:
When defining functions at the read-eval-print loop as shown here, a blank line is required to denote the end of a function definition (lines 3 and 6).
In defining a function in Python, the programmer must decide how they want the caller to assign argument values to parameters: by position (as in C), by keyword, or by a mixture of both. Readers with imperative programming experience are typically familiar with positional argument values, which are not prefaced with keywords, and where order matters. Let us consider keyword arguments and mixtures of both positional and keyword arguments.
Keyword arguments: There are two types of keyword arguments: named and unnamed.
named keyword arguments: The advantage of keyword arguments is that they need not conform to a strict order (as prescribed by a functional signature using positional arguments), and can have default values:
Note that order matters if you omit the keyword in the call:
unnamed keyword arguments: Unnamed keyword arguments are supplied to the function in the same way as named keyword arguments (i.e., as key–value pairs), but are available in the body of the function as adictionary:
Unnamed keyword arguments in Python are similar to variable argument lists in C:
Mixture of positional and named keyword arguments:
Note that the keyword arguments must be listed after all of the positional arguments in the argument list.
Mixture of positional and unnamed keyword arguments:5
Other Related Notes
If the arguments to a function are not available individually, they can be passed to a function in a list whose identifier is prefaced with a * (line 7):
Python supports function annotations, which, while optional, allow the programmer to associate arbitrary Python expressions with parameters and/or return value at compile time.
Python does not support traditional function overloading. When a programmer defines a function a second time, albeit with a new argument list, the second definition fully replaces the first definition rather than providing an alternative, overloaded definition.
Lambda functions (i.e., anonymous or literal functions) are introduced with lambda. They are often used, as in other languages, in concert with higher-order functions including map, which is built into Python as in Scheme:
These Python functions are the analogs of the following Scheme functions:
Anonymous functions are often used as arguments to higher-order functions (e.g., map) and are, hence, helpful. Python also supports the higher-order functions filter and reduce.
Python supports both first-class functions and first-class closures:
For more information, see Section 6.10.2.
gcd
factorial
fibonacci
reverse
Note that reverse can reverse a list containing values of any type.
member
Consider the following definition of a list member function in Python:
A local variable in Python can be used to introduce local binding for the purposes of avoiding recomputation of common subexpressions and creating nested functions for both protection and factoring out (so as to avoid passing and copying) arguments that remain constant between recursive function calls.
Local Binding
These functions are the Python analogs of the following Scheme functions:
Nested Functions
Since the function insertineach is intended to be only visible within, accessible within, and called by the powerset function, we can nest it within the powerset function:
The following is an example of using a nested function within the definition of a reverse function:
Unlike ML, but like Scheme and Haskell, Python allows a function to call a function that is defined below it:
This makes the definition of mutually recursive functions straightforward. For instance, consider the functions iseven and isodd, which rely on each other to determine if an integer is even or odd, respectively:
Note that more than two mutually recursive functions can be defined.
Consider the following definitions of a mergesort function.
Unnested, Unhidden, Flat Version
Nested, Hidden Version
Nested, Hidden Version Accepting a Comparison Operator as a Parameter
Final Version
The following is the final version of mergesort using nested, protected functions and accepting a comparison operator as a parameter that is factored out to avoid passing it between successive recursive calls. We also use a keyword argument for the comparison operator:
Notice also that we factored the argument compop out of the function merge in this version, since it is visible from an outer scope.
5. We use sys.stdout.write here rather than print to suppress a space from being automatically written between arguments to print.
3.22.216.254