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