basic_python/func.rst
author amit
Tue, 07 Sep 2010 11:03:27 +0530
changeset 123 7a16c80c584d
parent 47 e724f1ee6e51
permissions -rw-r--r--
Merging all the changes made bu madhu and punch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
Functional Approach
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
===================
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
*Functions* allow us to enclose a set of statements and call the function again
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
and again instead of repeating the group of statements everytime. Functions also
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
allow us to isolate a piece of code from all the other code and provides the
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
convenience of not polluting the global variables.
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
*Function* in python is defined with the keyword **def** followed by the name
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    10
of the function, in turn followed by a pair of parenthesis which encloses the
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    11
list of parameters to the function. The definition line ends with a ':'. The
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
definition line is followed by the body of the function intended by one block.
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    13
The *Function* must return a value::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    15
  def factorial(n):
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    16
    fact = 1
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    17
    for i in range(2, n):
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    18
      fact *= i
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    19
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    20
    return fact
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
The code snippet above defines a function with the name factorial, takes the
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    23
number for which the factorial must be computed, computes the factorial and
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    24
returns the value.
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    25
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    26
A *Function* once defined can be used or called anywhere else in the program. We
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    27
call a fucntion with its name followed by a pair of parenthesis which encloses
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    28
the arguments to the function.
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    29
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    30
The value that function returns can be assigned to a variable. Let's call the
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    31
above function and store the factorial in a variable::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    32
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    33
  fact5 = factorial(5)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    34
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    35
The value of fact5 will now be 120, which is the factorial of 5. Note that we
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    36
passed 5 as the argument to the function.
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    37
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    38
It may be necessary to document what the function does, for each of the function
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    39
to help the person who reads our code to understand it better. In order to do
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    40
this Python allows the first line of the function body to be a string. This
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    41
string is called as *Documentation String* or *docstring*. *docstrings* prove
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    42
to be very handy since there are number of tools which can pull out all the
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    43
docstrings from Python functions and generate the documentation automatically
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    44
from it. *docstrings* for functions can be written as follows::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    45
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    46
  def factorial(n):
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    47
    'Returns the factorial for the number n.'
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    48
    fact = 1
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    49
    for i in range(2, n):
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    50
      fact *= i
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    51
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    52
    return fact
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    53
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    54
An important point to note at this point is that, a function can return any
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    55
Python value or a Python object, which also includes a *Tuple*. A *Tuple* is
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    56
just a collection of values and those values themselves can be of any other
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    57
valid Python datatypes, including *Lists*, *Tuples*, *Dictionaries* among other
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    58
things. So effectively, if a function can return a tuple, it can return any
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    59
number of values through a tuple
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    60
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    61
Let us write a small function to swap two values::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    62
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    63
  def swap(a, b):
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    64
    return b, a
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    65
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    66
  c, d = swap(a, b)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    67
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    68
Function scope
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    69
---------------
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    70
The variables used inside the function are confined to the function's scope
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    71
and doesn't pollute the variables of the same name outside the scope of the
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    72
function. Also the arguments passed to the function are passed by-value if
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    73
it is of basic Python data type::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    74
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    75
  def cant_change(n):
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    76
    n = 10
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    77
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    78
  n = 5
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    79
  cant_change(n)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    80
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    81
Upon running this code, what do you think would have happened to value of n
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    82
which was assigned 5 before the function call? If you have already tried out
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    83
that snippet on the interpreter you already know that the value of n is not
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    84
changed. This is true of any immutable types of Python like *Numbers*, *Strings*
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    85
and *Tuples*. But when you pass mutable objects like *Lists* and *Dictionaries*
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    86
the values are manipulated even outside the function::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    87
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    88
  >>> def can_change(n):
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    89
  ...   n[1] = James
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    90
  ...
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    91
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    92
  >>> name = ['Mr.', 'Steve', 'Gosling']
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    93
  >>> can_change(name)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    94
  >>> name
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    95
  ['Mr.', 'James', 'Gosling']
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    96
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    97
If nothing is returned by the function explicitly, Python takes care to return
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    98
None when the funnction is called.
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    99
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   100
Default Arguments
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   101
-----------------
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   102
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   103
There may be situations where we need to allow the functions to take the
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   104
arguments optionally. Python allows us to define function this way by providing
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   105
a facility called *Default Arguments*. For example, we need to write a function
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   106
that returns a list of fibonacci numbers. Since our function cannot generate an
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   107
infinite list of fibonacci numbers, we need to specify the number of elements
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   108
that the fibonacci sequence must contain. Suppose, additionally, we want to the
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   109
function to return 10 numbers in the sequence if no option is specified we can
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   110
define the function as follows::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   111
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   112
  def fib(n=10):
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   113
    fib_list = [0, 1]
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   114
    for i in range(n - 2):
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   115
      next = fib_list[-2] + fib_list[-1]
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   116
      fib_list.append(next)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   117
    return fib_list
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   118
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   119
When we call this function, we can optionally specify the value for the
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   120
parameter n, during the call as an argument. Calling with no argument and
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   121
argument with n=5 returns the following fibonacci sequences::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   122
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   123
  fib()
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   124
  [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   125
  fib(5)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   126
  [0, 1, 1, 2, 3]
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   127
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   128
Keyword Arguments
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   129
-----------------
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   130
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   131
When a function takes a large number of arguments, it may be difficult to
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   132
remember the order of the parameters in the function definition or it may
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   133
be necessary to pass values to only certain parameters since others take
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   134
the default value. In either of these cases, Python provides the facility
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   135
of passing arguments by specifying the name of the parameter as defined in
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   136
the function definition. This is known as *Keyword Arguments*. 
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   137
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   138
In a function call, *Keyword arguments* can be used for each argument, in the
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   139
following fashion::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   140
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   141
  argument_name=argument_value
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   142
  Also denoted as: keyword=argument
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   143
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   144
  def wish(name='World', greetings='Hello'):
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   145
    print "%s, %s!" % (greetings, name)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   146
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   147
This function can be called in one of the following ways. It is important to
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   148
note that no restriction is imposed in the order in which *Keyword arguments*
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   149
can be specified. Also note, that we have combined *Keyword arguments* with
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   150
*Default arguments* in this example, however it is not necessary::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   151
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   152
  wish(name='Guido', greetings='Hey')
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   153
  wish(greetings='Hey', name='Guido')
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   154
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   155
Calling functions by specifying arguments in the order of parameters specified
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   156
in the function definition is called as *Positional arguments*, as opposed to
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   157
*Keyword arguments*. It is possible to use both *Positional arguments* and 
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   158
*Keyword arguments* in a single function call. But Python doesn't allow us to
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   159
bungle up both of them. The arguments to the function, in the call, must always
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   160
start with *Positional arguments* which is in turn followed by *Keyword
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   161
arguments*::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   162
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   163
  def my_func(x, y, z, u, v, w):
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   164
    # initialize variables.
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   165
    ...
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   166
    # do some stuff 
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   167
    ...
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   168
    # return the value
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   169
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   170
It is valid to call the above functions in the following ways::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   171
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   172
  my_func(10, 20, 30, u=1.0, v=2.0, w=3.0)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   173
  my_func(10, 20, 30, 1.0, 2.0, w=3.0)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   174
  my_func(10, 20, z=30, u=1.0, v=2.0, w=3.0)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   175
  my_func(x=10, y=20, z=30, u=1.0, v=2.0, w=3.0)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   176
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   177
Following lists some of the invalid calls::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   178
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   179
  my_func(10, 20, z=30, 1.0, 2.0, 3.0)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   180
  my_func(x=10, 20, z=30, 1.0, 2.0, 3.0)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   181
  my_func(x=10, y=20, z=30, u=1.0, v=2.0, 3.0)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   182
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   183
Parameter Packing and Unpacking
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   184
-------------------------------
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   185
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   186
The positional arguments passed to a function can be collected in a tuple
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   187
parameter and keyword arguments can be collected in a dictionary. Since keyword
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   188
arguments must always be the last set of arguments passed to a function, the
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   189
keyword dictionary parameter must be the last parameter. The function definition
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   190
must include a list explicit parameters, followed by tuple paramter collecting
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   191
parameter, whose name is preceded by a *****, for collecting positional
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   192
parameters, in turn followed by the dictionary collecting parameter, whose name
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   193
is preceded by a ****** ::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   194
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   195
  def print_report(title, *args, **name):
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   196
    """Structure of *args*
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   197
    (age, email-id)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   198
    Structure of *name*
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   199
    {
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   200
        'first': First Name
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   201
        'middle': Middle Name
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   202
        'last': Last Name
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   203
    }
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   204
    """
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   205
    
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   206
    print "Title: %s" % (title)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   207
    print "Full name: %(first)s %(middle)s %(last)s" % name
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   208
    print "Age: %d\nEmail-ID: %s" % args
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   209
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   210
The above function can be called as. Note, the order of keyword parameters can
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   211
be interchanged::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   212
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   213
  >>> print_report('Employee Report', 29, 'johny@example.com', first='Johny',
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   214
                   last='Charles', middle='Douglas')
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   215
  Title: Employee Report
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   216
  Full name: Johny Douglas Charles
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   217
  Age: 29
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   218
  Email-ID: johny@example.com
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   219
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   220
The reverse of this can also be achieved by using a very identical syntax while
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   221
calling the function. A tuple or a dictionary can be passed as arguments in
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   222
place of a list of *Positional arguments* or *Keyword arguments* respectively
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   223
using ***** or ****** ::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   224
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   225
  def print_report(title, age, email, first, middle, last):
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   226
    print "Title: %s" % (title)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   227
    print "Full name: %s %s %s" % (first, middle, last)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   228
    print "Age: %d\nEmail-ID: %s" % (age, email)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   229
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   230
  >>> args = (29, 'johny@example.com')
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   231
  >>> name = {
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   232
          'first': 'Johny',
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   233
          'middle': 'Charles',
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   234
          'last': 'Douglas'
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   235
          }
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   236
  >>> print_report('Employee Report', *args, **name)
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   237
  Title: Employee Report
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   238
  Full name: Johny Charles Douglas
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   239
  Age: 29
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   240
  Email-ID: johny@example.com
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   241
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   242
Nested Functions and Scopes
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   243
---------------------------
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   244
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   245
Python allows nesting one function inside another. This style of programming
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   246
turns out to be extremely flexible and powerful features when we use *Python
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   247
decorators*. We will not talk about decorators is beyond the scope of this
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   248
course. If you are interested in knowing more about *decorator programming* in
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   249
Python you are suggested to read:
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   250
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   251
| http://avinashv.net/2008/04/python-decorators-syntactic-sugar/
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   252
| http://personalpages.tds.net/~kent37/kk/00001.html
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   253
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   254
However, the following is an example for nested functions in Python::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   255
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   256
  def outer():
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   257
    print "Outer..."
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   258
    def inner():
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   259
      print "Inner..."
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   260
    print "Outer..."
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   261
    inner()
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   262
  
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   263
  >>> outer()
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   264
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   265
map, reduce and filter functions
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   266
--------------------------------
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   267
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   268
Python provides several built-in functions for convenience. The **map()**,
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   269
**reduce()** and **filter()** functions prove to be very useful with sequences like
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   270
*Lists*.
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   271
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   272
The **map** (*function*, *sequence*) function takes two arguments: *function*
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   273
and a *sequence* argument. The *function* argument must be the name of the
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   274
function which in turn takes a single argument, the individual element of the
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   275
*sequence*. The **map** function calls *function(item)*, for each item in the
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   276
sequence and returns a list of values, where each value is the value returned
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   277
by each call to *function(item)*. **map()** function allows to pass more than
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   278
one sequence. In this case, the first argument, *function* must take as many
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   279
arguments as the number of sequences passed. This function is called with each
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   280
corresponding element in the each of the sequences, or **None** if one of the
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   281
sequence is exhausted::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   282
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   283
  def square(x):
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   284
    return x*x
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   285
  
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   286
  >>> map(square, [1, 2, 3, 4])
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   287
  [1, 4, 9, 16]
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   288
  
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   289
  def mul(x, y):
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   290
    return x*y
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   291
  
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   292
  >>> map(mul, [1, 2, 3, 4], [6, 7, 8, 9])
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   293
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   294
The **filter** (*function*, *sequence*) function takes two arguments, similar to
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   295
the **map()** function. The **filter** function calls *function(item)*, for each
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   296
item in the sequence and returns all the elements in the sequence for which 
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   297
*function(item)* returned True::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   298
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   299
  def even(x):
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   300
    if x % 2:
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   301
      return True
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   302
    else:
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   303
      return False
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   304
  
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   305
  >>> filter(even, range(1, 10))
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   306
  [1, 3, 5, 7, 9]
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   307
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   308
The **reduce** (*function*, *sequence*) function takes two arguments, similar to
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   309
**map** function, however multiple sequences are not allowed. The **reduce**
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   310
function calls *function* with first two consecutive elements in the sequence,
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   311
obtains the result, calls *function* with the result and the subsequent element
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   312
in the sequence and so on until the end of the list and returns the final result::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   313
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   314
  def mul(x, y):
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   315
    return x*y
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   316
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   317
  >>> reduce(mul, [1, 2, 3, 4])
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   318
  24
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   319
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   320
List Comprehensions
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   321
~~~~~~~~~~~~~~~~~~~
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   322
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   323
List Comprehension is a convenvience utility provided by Python. It is a 
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   324
syntatic sugar to create *Lists*. Using *List Comprehensions* one can create
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   325
*Lists* from other type of sequential data structures or other *Lists* itself.
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   326
The syntax of *List Comprehensions* consists of a square brackets to indicate
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   327
the result is a *List* within which we include at least one **for** clause and
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   328
multiple **if** clauses. It will be more clear with an example::
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   329
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   330
  >>> num = [1, 2, 3]
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   331
  >>> sq = [x*x for x in num]
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   332
  >>> sq
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   333
  [1, 4, 9]
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   334
  >>> all_num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   335
  >>> even = [x for x in all_num if x%2 == 0]
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   336
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   337
The syntax used here is very clear from the way it is written. It can be 
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   338
translated into english as, "for each element x in the list all_num, 
e724f1ee6e51 Added and completed(first run) course hand out for functional approach.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   339
if remainder of x divided by 2 is 0, add x to the list."