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