A.7 User-Defined Functions

A.7.1 Simple User-Defined Functions

The following are some simple user-defined functions:

A set of 18 code lines in Python with simple user-defined functions.
Description

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).

A.7.2 Positional Vis-à-Vis Keyword Arguments

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:

      A set of 11 code lines in Python with named keyword arguments.
      Description

      Note that order matters if you omit the keyword in the call:

      A set of four code lines in Python without a keyword.
      Description
    • 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:

      A set of seven code lines in Python with unnamed keyword arguments.
      Description
      Continuation of the code in Python with unnamed keyword arguments consisting of 14 lines.
      Description

      Unnamed keyword arguments in Python are similar to variable argument lists in C:

      A set of 10 code lines in Python with unnamed keyword arguments that are similar to variable argument lists in C.
      Description
  • Mixture of positional and named keyword arguments:

    A set of 9 code lines in Python with a mixture of positional and named keyword arguments.
    Description
    Continuation of the code in Python with a mixture of positional and named keyword arguments consisting of three lines.
    Description

    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

    A set of 28 code lines in Python with a mixture of positional and unnamed keyword arguments.
    Description

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):

    A set of eight code lines in Python that consists of an identifier prefaced with an asterisk.
    Description
  • 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.

A.7.3 Lambda Functions

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:

A set of 38 code lines in Python with lambda and map functions.
Description

These Python functions are the analogs of the following Scheme functions:

A set of 23 code lines with Python functions that are analogs of Scheme functions.
Description

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.

A.7.4 Lexical Closures

Python supports both first-class functions and first-class closures:

A set of 12 code lines in Python with first-class functions and first-class closures.
Description
Continuation of the code in Python with first-class functions and first-class closures consisting of three lines.
Description

For more information, see Section 6.10.2.

A.7.5 More User-Defined Functions

gcd

A set of eight code lines in Python with the user-defined function g c d.
Description

factorial

A set of eight code lines in Python with the user-defined function factorial.
Description

fibonacci

A set of 18 code lines in Python with the user-defined function Fibonacci.
Description

reverse

A set of 21 code lines in Python with the user-defined function reverse.
Description

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 set of 29 code lines in Python with the user-defined function member.
Description
Continuation of the code in Python with the user-defined function False consisting of three lines.
Description

A.7.6 Local Binding and Nested Functions

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

A set of 29 code lines in Python with local binding.
Description

These functions are the Python analogs of the following Scheme functions:

A set of 11 code lines that are Python analogs of Scheme functions.
Description

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:

A set of 23 code lines in Python with nested functions.
Description

The following is an example of using a nested function within the definition of a reverse function:

A set of 23 code lines in Python with a nested function used within the definition of a reverse function.
Description

A.7.7 Mutual Recursion

Unlike ML, but like Scheme and Haskell, Python allows a function to call a function that is defined below it:

A set of eight code lines in Python with mutual recursion.
Description

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:

A set of 20 code lines in Python with is even and is odd functions.
Description

Note that more than two mutually recursive functions can be defined.

A.7.8 Putting It All Together: Mergesort

Consider the following definitions of a mergesort function.

Unnested, Unhidden, Flat Version

A set of eight code lines in Python with merge sort function in an unnested, unhidden, flat version.
Description
Continuation of the code in Python with merge sort function in an unnested, unhidden, flat version consisting of 25 lines.
Description

Nested, Hidden Version

A set of 23 code lines in Python with merge sort function in a nested and hidden version.
Description
Continuation of the code in Python with merge sort function in a nested and hidden version consisting of 10 lines.
Description

Nested, Hidden Version Accepting a Comparison Operator as a Parameter

A set of 35 code lines in Python with merge sort function in a nested and hidden version accepting a comparison operator as a parameter.
Description
Continuation of the code in Python with merge sort function in a nested and hidden version accepting a comparison operator as a parameter and consisting of two lines.
Description

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:

A set of 38 code lines in Python with merge sort function in the final version.
Description

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.210.143