web/html/backup/func.html
author amit@thunder
Mon, 25 Jan 2010 18:56:45 +0530
changeset 0 8083d21c0020
permissions -rwxr-xr-x
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><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
     3
<title>Chapter 9. Finding and fixing mistakes</title>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     4
<link rel="stylesheet" href="/support/styles.css" type="text/css">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     5
<meta name="generator" content="DocBook XSL Stylesheets V1.74.3"><link rel="home" href="index.html" title="Mercurial: The Definitive Guide">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     6
<link rel="up" href="index.html" title="Mercurial: The Definitive Guide">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     7
<link rel="prev" href="managing-releases-and-branchy-development.html" title="Chapter 8. Managing releases and branchy development">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     8
<link rel="next" href="handling-repository-events-with-hooks.html" title="Chapter 10. Handling repository events with hooks"><link rel="alternate" type="application/atom+xml" title="Comments" href="/feeds/comments/">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     9
<link rel="shortcut icon" type="image/png" href="/support/figs/favicon.png">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    10
<script type="text/javascript" src="/support/jquery-min.js"></script>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    11
<script type="text/javascript" src="/support/form.js"></script>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    12
<script type="text/javascript" src="/support/hsbook.js"></script></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">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    14
<!--<div class="navheader">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    15
<table width="100%" summary="Navigation header">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    16
<tr><th colspan="3" align="center">Chapter 14. Adding functionality with extensions</th></tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    17
<tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    18
<td width="20%" align="left">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    19
<a accesskey="p" href="advanced-uses-of-mercurial-queues.html">Prev</a> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    20
<th width="60%" align="center"> </th>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    21
<td width="20%" align="right"> <a accesskey="n" href="migrating-to-mercurial.html">Next</a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    22
</td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    23
</tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    24
</table>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    25
<hr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    26
</div> -->
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    27
<div class="chapter" title="Chapter 14. Adding functionality with extensions">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    28
<div class="titlepage"><div><div><h2 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    29
<a name="chap:hgext"></a>Chapter 14. Adding functionality with extensions</h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    30
<div class="toc">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    31
<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
    32
<dl>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    33
<dt><span class="sect1"><a href="adding-functionality-with-extensions.html#sec:hgext:inotify">14.1. Improve performance with the <code class="literal">inotify</code> extension</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    34
<dt><span class="sect1"><a href="adding-functionality-with-extensions.html#sec:hgext:extdiff">14.2. Flexible diff support with the <code class="literal">extdiff</code> extension</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    35
<dd><dl><dt><span class="sect2"><a href="adding-functionality-with-extensions.html#id3071699">14.2.1. Defining command aliases</a></span></dt></dl></dd>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    36
<dt><span class="sect1"><a href="adding-functionality-with-extensions.html#sec:hgext:transplant">14.3. Cherrypicking changes with the <code class="literal">transplant</code> extension</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    37
<dt><span class="sect1"><a href="adding-functionality-with-extensions.html#sec:hgext:patchbomb">14.4. Send changes via email with the <code class="literal">patchbomb</code> extension</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    38
<dd><dl><dt><span class="sect2"><a href="adding-functionality-with-extensions.html#id3072184">14.4.1. Changing the behavior of patchbombs</a></span></dt></dl></dd>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    39
</dl>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    40
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    41
<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
    42
<dl>
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="#default-arguments">1. Default Arguments</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="#keyword-arguments">2. Keyword Arguments</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    45
<dt><span class="section"><a href="#parameter-packing-and-unpacking">3. 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
    46
<dt><span class="section"><a href="#nested-functions-and-scopes">4. 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
    47
<dt><span class="section"><a href="#map-reduce-and-filter-functions">5. 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
    48
<dd><dl><dt><span class="section"><a href="#list-comprehensions">5.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
    49
</dl>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    50
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    51
<p id="x_74b">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    52
    <span class="emphasis"><em>Functions</em></span> allow us to enclose a set of statements and call the function again and again instead of repeating the group of statements everytime. Functions also allow us to isolate a piece of code from all the other code and provides the convenience of not polluting the global variables.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    53
  </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    54
<p id="x_74c"<a name="x_74c"></a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    55
    <span class="emphasis"><em>Function</em></span> in python is defined with the keyword 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    56
    <span class="strong"><strong>def</strong></span> followed by the name of the function, in turn followed by a pair of parenthesis which encloses the list of parameters to the function. The definition line ends with a ':'. The definition line is followed by the body of the function intended by one block. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    57
    <span class="emphasis"><em>Function</em></span> must return a value:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    58
  </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    59
<pre class="programlisting">def factorial(n):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    60
  fact = 1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    61
  for i in range(2, n):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    62
    fact *= i
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    63
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    64
  return fact
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    65
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    66
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    67
<p><a name="x_74d"></a>The code snippet above defines a function with the name factorial, takes the number for which the factorial must be computed, computes the factorial and returns the value.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    68
<p><a name="x_74e"></a>A 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    69
    <span class="emphasis"><em>Function</em></span> once defined can be used or called anywhere else in the program. We call a fucntion with its name followed by a pair of parenthesis which encloses the arguments to the function.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    70
  </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    71
<p><a name="x_74f"></a>The value that function returns can be assigned to a variable. Let's call the 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
    72
<pre class="programlisting">fact5 = factorial(5)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    73
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    74
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    75
<p><a name="x_750"></a>The value of fact5 will now be 120, which is the factorial of 5. Note that we 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
    76
<p><a name="x_751"></a>It may be necessary to document what the function does, for each of the function to help the person who reads our code to understand it better. In order to do this Python allows the first line of the function body to be a string. This string is called as 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    77
    <span class="emphasis"><em>Documentation String</em></span> or 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    78
    <span class="emphasis"><em>docstring</em></span>. 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    79
    <span class="emphasis"><em>docstrings</em></span> prove to be very handy since there are number of tools which can pull out all the docstrings from Python functions and generate the documentation automatically from it. 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    80
    <span class="emphasis"><em>docstrings</em></span> for functions can be written as follows:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    81
  </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    82
<pre class="programlisting">def factorial(n):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    83
  '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
    84
  fact = 1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    85
  for i in range(2, n):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    86
    fact *= i
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    87
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    88
  return fact
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    89
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    90
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    91
<p><a name="x_752"></a>An important point to note at this point is that, a function can return any Python value or a Python object, which also includes a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    92
    <span class="emphasis"><em>Tuple</em></span>. A 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    93
    <span class="emphasis"><em>Tuple</em></span> is just a collection of values and those values themselves can be of any other valid Python datatypes, including 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    94
    <span class="emphasis"><em>Lists</em></span>, 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    95
    <span class="emphasis"><em>Tuples</em></span>, 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    96
    <span class="emphasis"><em>Dictionaries</em></span> among other things. So effectively, if a function can return a tuple, it can return any number of values through a tuple
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    97
  </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    98
<p><a name="x_753"></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
    99
<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
   100
  return b, a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   101
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   102
c, d = swap(a, b)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   103
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   104
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   105
<p><a name="x_754"></a>Function scope --------------- The variables used inside the function are confined to the function's scope and doesn't pollute the variables of the same name outside the scope of the function. Also the arguments passed to the function are passed by-value if 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)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   111
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   112
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   113
<p><a name="x_755"></a>Upon running this code, what do you think would have happened to value of n which was assigned 5 before the function call? If you have already tried out that snippet on the interpreter you already know that the value of n is not changed. This is true of any immutable types of Python like 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   114
    <span class="emphasis"><em>Numbers</em></span>, 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   115
    <span class="emphasis"><em>Strings</em></span> and 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   116
    <span class="emphasis"><em>Tuples</em></span>. But when you pass mutable objects like 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   117
    <span class="emphasis"><em>Lists</em></span> and 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   118
    <span class="emphasis"><em>Dictionaries</em></span> the values are manipulated even outside the function:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   119
  </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   120
<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
   121
...   n[1] = James
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   122
...
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   123
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   124
&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
   125
&gt;&gt;&gt; can_change(name)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   126
&gt;&gt;&gt; name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   127
['Mr.', 'James', 'Gosling']
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   128
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   129
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   130
<p><a name="x_756"></a>If nothing is returned by the function explicitly, Python takes care to return 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
   131
<div class="section" title="1. Default Arguments">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   132
<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
   133
<a name="default-arguments"></a>1. Default Arguments</h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   134
<p><a name="x_757"></a>There may be situations where we need to allow the functions to take the arguments optionally. Python allows us to define function this way by providing a facility called 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   135
      <span class="emphasis"><em>Default Arguments</em></span>. For example, we need to write a function that returns a list of fibonacci numbers. Since our function cannot generate an infinite list of fibonacci numbers, we need to specify the number of elements that the fibonacci sequence must contain. Suppose, additionally, we want to the function to return 10 numbers in the sequence if no option is specified we can define the function as follows:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   136
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   137
<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
   138
  fib_list = [0, 1]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   139
  for i in range(n - 2):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   140
    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
   141
    fib_list.append(next)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   142
  return fib_list
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   143
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   144
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   145
<p><a name="x_758"></a>When we call this function, we can optionally specify the value for the parameter n, during the call as an argument. Calling with no argument and 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
   146
<pre class="programlisting">fib()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   147
[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
   148
fib(5)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   149
[0, 1, 1, 2, 3]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   150
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   151
</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="2. 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="keyword-arguments"></a>2. 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><a name="x_759"></a>When a function takes a large number of arguments, it may be difficult to remember the order of the parameters in the function definition or it may be necessary to pass values to only certain parameters since others take the default value. In either of these cases, Python provides the facility of passing arguments by specifying the name of the parameter as defined in the function definition. This is known as 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   157
      <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
   158
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   159
<p><a name="x_75a"></a>In a function call, 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   160
      <span class="emphasis"><em>Keyword arguments</em></span> can be used for each argument, in the following fashion:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   161
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   162
<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
   163
Also denoted as: keyword=argument
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   164
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   165
def wish(name='World', greetings='Hello'):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   166
  print "%s, %s!" % (greetings, name)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   167
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   168
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   169
<p><a name="x_75b"></a>This function can be called in one of the following ways. It is important to note that no restriction is imposed in the order in which 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   170
      <span class="emphasis"><em>Keyword arguments</em></span> can be specified. Also note, that we have combined 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   171
      <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:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   173
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   174
<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
   175
wish(greetings='Hey', name='Guido')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   176
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   177
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   178
<p><a name="x_75c"></a>Calling functions by specifying arguments in the order of parameters specified in the function definition is called as 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   179
      <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
   180
      <span class="emphasis"><em>Keyword arguments</em></span>. It is possible to use both 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   181
      <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
   182
      <span class="emphasis"><em>Keyword arguments</em></span> in a single function call. But Python doesn't allow us to bungle up both of them. The arguments to the function, in the call, must always start with 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   183
      <span class="emphasis"><em>Positional arguments</em></span> which is in turn followed by 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   184
      <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
   185
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   186
<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
   187
  # initialize variables.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   188
  ...
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   189
  # do some stuff 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   190
  ...
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   191
  # return the value
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   192
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   193
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   194
<p><a name="x_75d"></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
   195
<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
   196
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
   197
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
   198
my_func(x=10, y=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
   199
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   200
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   201
<p><a name="x_75e"></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
   202
<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
   203
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
   204
my_func(x=10, y=20, z=30, u=1.0, v=2.0, 3.0)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   205
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   206
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   207
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   208
<div class="section" title="3. Parameter Packing and Unpacking">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   209
<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
   210
<a name="parameter-packing-and-unpacking"></a>3. 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
   211
<p><a name="x_75f"></a>The positional arguments passed to a function can be collected in a tuple parameter and keyword arguments can be collected in a dictionary. Since keyword arguments must always be the last set of arguments passed to a function, the keyword dictionary parameter must be the last parameter. The function definition must include a list explicit parameters, followed by tuple paramter collecting parameter, whose name is preceded by a *****, for collecting positional parameters, in turn followed by the dictionary collecting parameter, whose name is preceded by a ****** :</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   212
<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
   213
  """Structure of *args*
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   214
  (age, email-id)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   215
  Structure of *name*
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   216
  {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   217
      'first': First Name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   218
      'middle': Middle Name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   219
      'last': Last Name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   220
  }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   221
  """
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   222
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   223
  print "Title: %s" % (title)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   224
  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
   225
  print "Age: %d\nEmail-ID: %s" % args
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   226
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   227
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   228
<p><a name="x_760"></a>The above function can be called as. Note, the order of keyword parameters can be interchanged:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   229
<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
   230
                 last='Charles', middle='Douglas')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   231
Title: Employee Report
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   232
Full name: Johny Douglas Charles
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   233
Age: 29
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   234
Email-ID: johny@example.com
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   235
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   236
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   237
<p><a name="x_761"></a>The reverse of this can also be achieved by using a very identical syntax while calling the function. A tuple or a dictionary can be passed as arguments in place of a list of *Positional arguments* or *Keyword arguments* respectively using ***** or ****** :</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   238
<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
   239
  print "Title: %s" % (title)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   240
  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
   241
  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
   242
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   243
&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
   244
&gt;&gt;&gt; name = {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   245
        'first': 'Johny',
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   246
        'middle': 'Charles',
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   247
        'last': 'Douglas'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   248
        }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   249
&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
   250
Title: Employee Report
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   251
Full name: Johny Charles Douglas
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   252
Age: 29
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   253
Email-ID: johny@example.com
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   254
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   255
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   256
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   257
<div class="section" title="4. Nested Functions and Scopes">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   258
<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
   259
<a name="nested-functions-and-scopes"></a>4. 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
   260
<p><a name="x_762"></a>Python allows nesting one function inside another. This style of programming turns out to be extremely flexible and powerful features when we use 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   261
      <span class="emphasis"><em>Python decorators</em></span>. We will not talk about decorators is beyond the scope of this course. If you are interested in knowing more about 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   262
      <span class="emphasis"><em>decorator programming</em></span> in Python you are suggested to read:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   263
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   264
<div class="literallayout"><p><br>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   265
      <a class="ulink" href="http://avinashv.net/2008/04/python-decorators-syntactic-sugar" target="_top">http://avinashv.net/2008/04/python-decorators-syntactic-sugar</a>/<br>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   266
      <a class="ulink" href="http://personalpages.tds.net" target="_top">http://personalpages.tds.net</a>/~kent37/kk/00001.html<br>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   267
    </p></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   268
<p><a name="x_763"></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
   269
<pre class="programlisting">def outer():
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   270
  print "Outer..."
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   271
  def inner():
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   272
    print "Inner..."
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   273
  print "Outer..."
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   274
  inner()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   275
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   276
&gt;&gt;&gt; outer()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   277
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   278
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   279
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   280
<div class="section" title="5. map, reduce and filter functions">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   281
<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
   282
<a name="map-reduce-and-filter-functions"></a>5. 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
   283
<p><a name="x_764"></a>Python provides several built-in functions for convenience. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   284
      <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
   285
      <span class="strong"><strong>reduce()</strong></span> and 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   286
      <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
   287
      <span class="emphasis"><em>Lists</em></span>.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   288
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   289
<p><a name="x_765"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   290
      <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
   291
      <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
   292
      <span class="emphasis"><em>sequence</em></span>) function takes two arguments: 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   293
      <span class="emphasis"><em>function</em></span> and a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   294
      <span class="emphasis"><em>sequence</em></span> argument. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   295
      <span class="emphasis"><em>function</em></span> argument must be the name of the 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
   296
      <span class="emphasis"><em>sequence</em></span>. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   297
      <span class="strong"><strong>map</strong></span> function calls 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   298
      <span class="emphasis"><em>function(item)</em></span>, for each item in the sequence and returns a list of values, where each value is the value returned by each call to 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   299
      <span class="emphasis"><em>function(item)</em></span>. 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   300
      <span class="strong"><strong>map()</strong></span> function allows to pass more than one sequence. In this case, the first argument, 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   301
      <span class="emphasis"><em>function</em></span> must take as many arguments as the number of sequences passed. This function is called with each corresponding element in the each of the sequences, or 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   302
      <span class="strong"><strong>None</strong></span> if one of the sequence is exhausted:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   303
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   304
<pre class="programlisting">def square(x):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   305
  return x*x
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   306
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   307
&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
   308
[1, 4, 9, 16]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   309
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   310
def mul(x, y):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   311
  return x*y
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   312
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   313
&gt;&gt;&gt; map(mul, [1, 2, 3, 4], [6, 7, 8, 9])
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   314
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   315
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   316
<p><a name="x_766"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   317
      <span class="strong"><strong>filter</strong></span> (
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   318
      <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
   319
      <span class="emphasis"><em>sequence</em></span>) function takes two arguments, similar to the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   320
      <span class="strong"><strong>map()</strong></span> function. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   321
      <span class="strong"><strong>filter</strong></span> function calls 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   322
      <span class="emphasis"><em>function(item)</em></span>, for each 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
   323
      <span class="emphasis"><em>function(item)</em></span> returned True:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   324
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   325
<pre class="programlisting">def even(x):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   326
  if x % 2:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   327
    return True
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   328
  else:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   329
    return False
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   330
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   331
&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
   332
[1, 3, 5, 7, 9]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   333
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   334
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   335
<p><a name="x_767"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   336
      <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
   337
      <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
   338
      <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
   339
      <span class="strong"><strong>map</strong></span> function, however multiple sequences are not allowed. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   340
      <span class="strong"><strong>reduce</strong></span> function calls 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   341
      <span class="emphasis"><em>function</em></span> with first two consecutive elements in the sequence, obtains the result, calls 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   342
      <span class="emphasis"><em>function</em></span> with the result and the subsequent element in the sequence and so on until the end of the list and returns the final result:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   343
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   344
<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
   345
  return x*y
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   346
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   347
&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
   348
24
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   349
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   350
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   351
<div class="section" title="5.1. List Comprehensions">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   352
<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
   353
<a name="list-comprehensions"></a>5.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
   354
<p><a name="x_768"></a>List Comprehension is a convenvience utility provided by Python. It is a syntatic sugar to create 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   355
        <span class="emphasis"><em>Lists</em></span>. Using 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   356
        <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
   357
        <span class="emphasis"><em>Lists</em></span> from other type of sequential data structures or other 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   358
        <span class="emphasis"><em>Lists</em></span> itself. The syntax of 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   359
        <span class="emphasis"><em>List Comprehensions</em></span> consists of a square brackets to indicate the result is a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   360
        <span class="emphasis"><em>List</em></span> within which we include at least one 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   361
        <span class="strong"><strong>for</strong></span> clause and multiple 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   362
        <span class="strong"><strong>if</strong></span> clauses. It will be more clear with an example:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   363
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   364
<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
   365
&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
   366
&gt;&gt;&gt; sq
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   367
[1, 4, 9]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   368
&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
   369
&gt;&gt;&gt; even = [x for x in all_num if x%2 == 0]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   370
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   371
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   372
<p><a name="x_769"></a>The syntax used here is very clear from the way it is written. It can be translated into english as, "for each element x in the list all_num, 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
   373
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   374
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   375
</div></body>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   376
</html>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   377
<html>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   378
<head>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   379
<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
   380
<title>Functional Approach</title>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   381
<link rel="stylesheet" href="hgbook.css" type="text/css">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   382
<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
   383
</head>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   384
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="article" title="Functional Approach">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   385
<div class="titlepage">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   386
<div><div><h2 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   387
<a name="id2760558"></a>Functional Approach</h2></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   388
<hr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   389
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   390
<div class="toc">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   391
<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
   392
<dl>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   393
<dt><span class="section"><a href="#default-arguments">1. Default Arguments</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   394
<dt><span class="section"><a href="#keyword-arguments">2. Keyword Arguments</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   395
<dt><span class="section"><a href="#parameter-packing-and-unpacking">3. 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
   396
<dt><span class="section"><a href="#nested-functions-and-scopes">4. 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
   397
<dt><span class="section"><a href="#map-reduce-and-filter-functions">5. 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
   398
<dd><dl><dt><span class="section"><a href="#list-comprehensions">5.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
   399
</dl>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   400
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   401
<p><a name="x_74b"></a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   402
    <span class="emphasis"><em>Functions</em></span> allow us to enclose a set of statements and call the function again and again instead of repeating the group of statements everytime. Functions also allow us to isolate a piece of code from all the other code and provides the convenience of not polluting the global variables.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   403
  </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   404
<p><a name="x_74c"></a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   405
    <span class="emphasis"><em>Function</em></span> in python is defined with the keyword 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   406
    <span class="strong"><strong>def</strong></span> followed by the name of the function, in turn followed by a pair of parenthesis which encloses the list of parameters to the function. The definition line ends with a ':'. The definition line is followed by the body of the function intended by one block. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   407
    <span class="emphasis"><em>Function</em></span> must return a value:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   408
  </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   409
<pre class="programlisting">def factorial(n):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   410
  fact = 1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   411
  for i in range(2, n):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   412
    fact *= i
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   413
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   414
  return fact
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   415
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   416
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   417
<p><a name="x_74d"></a>The code snippet above defines a function with the name factorial, takes the number for which the factorial must be computed, computes the factorial and returns the value.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   418
<p><a name="x_74e"></a>A 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   419
    <span class="emphasis"><em>Function</em></span> once defined can be used or called anywhere else in the program. We call a fucntion with its name followed by a pair of parenthesis which encloses the arguments to the function.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   420
  </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   421
<p><a name="x_74f"></a>The value that function returns can be assigned to a variable. Let's call the 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
   422
<pre class="programlisting">fact5 = factorial(5)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   423
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   424
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   425
<p><a name="x_750"></a>The value of fact5 will now be 120, which is the factorial of 5. Note that we 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
   426
<p><a name="x_751"></a>It may be necessary to document what the function does, for each of the function to help the person who reads our code to understand it better. In order to do this Python allows the first line of the function body to be a string. This string is called as 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   427
    <span class="emphasis"><em>Documentation String</em></span> or 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   428
    <span class="emphasis"><em>docstring</em></span>. 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   429
    <span class="emphasis"><em>docstrings</em></span> prove to be very handy since there are number of tools which can pull out all the docstrings from Python functions and generate the documentation automatically from it. 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   430
    <span class="emphasis"><em>docstrings</em></span> for functions can be written as follows:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   431
  </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   432
<pre class="programlisting">def factorial(n):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   433
  '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
   434
  fact = 1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   435
  for i in range(2, n):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   436
    fact *= i
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   437
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   438
  return fact
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   439
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   440
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   441
<p><a name="x_752"></a>An important point to note at this point is that, a function can return any Python value or a Python object, which also includes a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   442
    <span class="emphasis"><em>Tuple</em></span>. A 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   443
    <span class="emphasis"><em>Tuple</em></span> is just a collection of values and those values themselves can be of any other valid Python datatypes, including 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   444
    <span class="emphasis"><em>Lists</em></span>, 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   445
    <span class="emphasis"><em>Tuples</em></span>, 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   446
    <span class="emphasis"><em>Dictionaries</em></span> among other things. So effectively, if a function can return a tuple, it can return any number of values through a tuple
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   447
  </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   448
<p><a name="x_753"></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
   449
<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
   450
  return b, a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   451
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   452
c, d = swap(a, b)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   453
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   454
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   455
<p><a name="x_754"></a>Function scope --------------- The variables used inside the function are confined to the function's scope and doesn't pollute the variables of the same name outside the scope of the function. Also the arguments passed to the function are passed by-value if 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
   456
<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
   457
  n = 10
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   458
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   459
n = 5
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   460
cant_change(n)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   461
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   462
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   463
<p><a name="x_755"></a>Upon running this code, what do you think would have happened to value of n which was assigned 5 before the function call? If you have already tried out that snippet on the interpreter you already know that the value of n is not changed. This is true of any immutable types of Python like 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   464
    <span class="emphasis"><em>Numbers</em></span>, 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   465
    <span class="emphasis"><em>Strings</em></span> and 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   466
    <span class="emphasis"><em>Tuples</em></span>. But when you pass mutable objects like 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   467
    <span class="emphasis"><em>Lists</em></span> and 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   468
    <span class="emphasis"><em>Dictionaries</em></span> the values are manipulated even outside the function:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   469
  </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   470
<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
   471
...   n[1] = James
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   472
...
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   473
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   474
&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
   475
&gt;&gt;&gt; can_change(name)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   476
&gt;&gt;&gt; name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   477
['Mr.', 'James', 'Gosling']
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   478
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   479
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   480
<p><a name="x_756"></a>If nothing is returned by the function explicitly, Python takes care to return 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
   481
<div class="section" title="1. Default Arguments">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   482
<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
   483
<a name="default-arguments"></a>1. Default Arguments</h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   484
<p><a name="x_757"></a>There may be situations where we need to allow the functions to take the arguments optionally. Python allows us to define function this way by providing a facility called 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   485
      <span class="emphasis"><em>Default Arguments</em></span>. For example, we need to write a function that returns a list of fibonacci numbers. Since our function cannot generate an infinite list of fibonacci numbers, we need to specify the number of elements that the fibonacci sequence must contain. Suppose, additionally, we want to the function to return 10 numbers in the sequence if no option is specified we can define the function as follows:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   486
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   487
<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
   488
  fib_list = [0, 1]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   489
  for i in range(n - 2):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   490
    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
   491
    fib_list.append(next)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   492
  return fib_list
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   493
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   494
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   495
<p><a name="x_758"></a>When we call this function, we can optionally specify the value for the parameter n, during the call as an argument. Calling with no argument and 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
   496
<pre class="programlisting">fib()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   497
[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
   498
fib(5)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   499
[0, 1, 1, 2, 3]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   500
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   501
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   502
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   503
<div class="section" title="2. Keyword Arguments">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   504
<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
   505
<a name="keyword-arguments"></a>2. Keyword Arguments</h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   506
<p><a name="x_759"></a>When a function takes a large number of arguments, it may be difficult to remember the order of the parameters in the function definition or it may be necessary to pass values to only certain parameters since others take the default value. In either of these cases, Python provides the facility of passing arguments by specifying the name of the parameter as defined in the function definition. This is known as 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   507
      <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
   508
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   509
<p><a name="x_75a"></a>In a function call, 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   510
      <span class="emphasis"><em>Keyword arguments</em></span> can be used for each argument, in the following fashion:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   511
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   512
<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
   513
Also denoted as: keyword=argument
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   514
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   515
def wish(name='World', greetings='Hello'):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   516
  print "%s, %s!" % (greetings, name)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   517
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   518
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   519
<p><a name="x_75b"></a>This function can be called in one of the following ways. It is important to note that no restriction is imposed in the order in which 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   520
      <span class="emphasis"><em>Keyword arguments</em></span> can be specified. Also note, that we have combined 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   521
      <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
   522
      <span class="emphasis"><em>Default arguments</em></span> in this example, however it is not necessary:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   523
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   524
<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
   525
wish(greetings='Hey', name='Guido')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   526
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   527
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   528
<p><a name="x_75c"></a>Calling functions by specifying arguments in the order of parameters specified in the function definition is called as 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   529
      <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
   530
      <span class="emphasis"><em>Keyword arguments</em></span>. It is possible to use both 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   531
      <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
   532
      <span class="emphasis"><em>Keyword arguments</em></span> in a single function call. But Python doesn't allow us to bungle up both of them. The arguments to the function, in the call, must always start with 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   533
      <span class="emphasis"><em>Positional arguments</em></span> which is in turn followed by 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   534
      <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
   535
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   536
<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
   537
  # initialize variables.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   538
  ...
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   539
  # do some stuff 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   540
  ...
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   541
  # return the value
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   542
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   543
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   544
<p><a name="x_75d"></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
   545
<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
   546
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
   547
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
   548
my_func(x=10, y=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
   549
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   550
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   551
<p><a name="x_75e"></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
   552
<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
   553
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
   554
my_func(x=10, y=20, z=30, u=1.0, v=2.0, 3.0)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   555
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   556
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   557
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   558
<div class="section" title="3. Parameter Packing and Unpacking">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   559
<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
   560
<a name="parameter-packing-and-unpacking"></a>3. 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
   561
<p><a name="x_75f"></a>The positional arguments passed to a function can be collected in a tuple parameter and keyword arguments can be collected in a dictionary. Since keyword arguments must always be the last set of arguments passed to a function, the keyword dictionary parameter must be the last parameter. The function definition must include a list explicit parameters, followed by tuple paramter collecting parameter, whose name is preceded by a *****, for collecting positional parameters, in turn followed by the dictionary collecting parameter, whose name is preceded by a ****** :</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   562
<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
   563
  """Structure of *args*
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   564
  (age, email-id)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   565
  Structure of *name*
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   566
  {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   567
      'first': First Name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   568
      'middle': Middle Name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   569
      'last': Last Name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   570
  }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   571
  """
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   572
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   573
  print "Title: %s" % (title)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   574
  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
   575
  print "Age: %d\nEmail-ID: %s" % args
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   576
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   577
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   578
<p><a name="x_760"></a>The above function can be called as. Note, the order of keyword parameters can be interchanged:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   579
<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
   580
                 last='Charles', middle='Douglas')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   581
Title: Employee Report
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   582
Full name: Johny Douglas Charles
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   583
Age: 29
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   584
Email-ID: johny@example.com
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   585
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   586
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   587
<p><a name="x_761"></a>The reverse of this can also be achieved by using a very identical syntax while calling the function. A tuple or a dictionary can be passed as arguments in place of a list of *Positional arguments* or *Keyword arguments* respectively using ***** or ****** :</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   588
<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
   589
  print "Title: %s" % (title)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   590
  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
   591
  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
   592
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   593
&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
   594
&gt;&gt;&gt; name = {
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   595
        'first': 'Johny',
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   596
        'middle': 'Charles',
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   597
        'last': 'Douglas'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   598
        }
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   599
&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
   600
Title: Employee Report
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   601
Full name: Johny Charles Douglas
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   602
Age: 29
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   603
Email-ID: johny@example.com
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   604
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   605
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   606
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   607
<div class="section" title="4. Nested Functions and Scopes">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   608
<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
   609
<a name="nested-functions-and-scopes"></a>4. 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
   610
<p><a name="x_762"></a>Python allows nesting one function inside another. This style of programming turns out to be extremely flexible and powerful features when we use 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   611
      <span class="emphasis"><em>Python decorators</em></span>. We will not talk about decorators is beyond the scope of this course. If you are interested in knowing more about 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   612
      <span class="emphasis"><em>decorator programming</em></span> in Python you are suggested to read:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   613
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   614
<div class="literallayout"><p><br>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   615
      <a class="ulink" href="http://avinashv.net/2008/04/python-decorators-syntactic-sugar" target="_top">http://avinashv.net/2008/04/python-decorators-syntactic-sugar</a>/<br>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   616
      <a class="ulink" href="http://personalpages.tds.net" target="_top">http://personalpages.tds.net</a>/~kent37/kk/00001.html<br>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   617
    </p></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   618
<p><a name="x_763"></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
   619
<pre class="programlisting">def outer():
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   620
  print "Outer..."
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   621
  def inner():
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   622
    print "Inner..."
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   623
  print "Outer..."
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   624
  inner()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   625
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   626
&gt;&gt;&gt; outer()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   627
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   628
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   629
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   630
<div class="section" title="5. map, reduce and filter functions">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   631
<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
   632
<a name="map-reduce-and-filter-functions"></a>5. 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
   633
<p><a name="x_764"></a>Python provides several built-in functions for convenience. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   634
      <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
   635
      <span class="strong"><strong>reduce()</strong></span> and 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   636
      <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
   637
      <span class="emphasis"><em>Lists</em></span>.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   638
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   639
<p><a name="x_765"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   640
      <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
   641
      <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
   642
      <span class="emphasis"><em>sequence</em></span>) function takes two arguments: 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   643
      <span class="emphasis"><em>function</em></span> and a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   644
      <span class="emphasis"><em>sequence</em></span> argument. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   645
      <span class="emphasis"><em>function</em></span> argument must be the name of the 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
   646
      <span class="emphasis"><em>sequence</em></span>. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   647
      <span class="strong"><strong>map</strong></span> function calls 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   648
      <span class="emphasis"><em>function(item)</em></span>, for each item in the sequence and returns a list of values, where each value is the value returned by each call to 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   649
      <span class="emphasis"><em>function(item)</em></span>. 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   650
      <span class="strong"><strong>map()</strong></span> function allows to pass more than one sequence. In this case, the first argument, 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   651
      <span class="emphasis"><em>function</em></span> must take as many arguments as the number of sequences passed. This function is called with each corresponding element in the each of the sequences, or 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   652
      <span class="strong"><strong>None</strong></span> if one of the sequence is exhausted:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   653
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   654
<pre class="programlisting">def square(x):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   655
  return x*x
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   656
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   657
&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
   658
[1, 4, 9, 16]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   659
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   660
def mul(x, y):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   661
  return x*y
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   662
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   663
&gt;&gt;&gt; map(mul, [1, 2, 3, 4], [6, 7, 8, 9])
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   664
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   665
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   666
<p><a name="x_766"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   667
      <span class="strong"><strong>filter</strong></span> (
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   668
      <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
   669
      <span class="emphasis"><em>sequence</em></span>) function takes two arguments, similar to the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   670
      <span class="strong"><strong>map()</strong></span> function. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   671
      <span class="strong"><strong>filter</strong></span> function calls 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   672
      <span class="emphasis"><em>function(item)</em></span>, for each 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
   673
      <span class="emphasis"><em>function(item)</em></span> returned True:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   674
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   675
<pre class="programlisting">def even(x):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   676
  if x % 2:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   677
    return True
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   678
  else:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   679
    return False
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   680
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   681
&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
   682
[1, 3, 5, 7, 9]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   683
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   684
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   685
<p><a name="x_767"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   686
      <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
   687
      <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
   688
      <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
   689
      <span class="strong"><strong>map</strong></span> function, however multiple sequences are not allowed. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   690
      <span class="strong"><strong>reduce</strong></span> function calls 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   691
      <span class="emphasis"><em>function</em></span> with first two consecutive elements in the sequence, obtains the result, calls 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   692
      <span class="emphasis"><em>function</em></span> with the result and the subsequent element in the sequence and so on until the end of the list and returns the final result:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   693
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   694
<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
   695
  return x*y
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   696
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   697
&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
   698
24
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   699
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   700
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   701
<div class="section" title="5.1. List Comprehensions">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   702
<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
   703
<a name="list-comprehensions"></a>5.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
   704
<p><a name="x_768"></a>List Comprehension is a convenvience utility provided by Python. It is a syntatic sugar to create 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   705
        <span class="emphasis"><em>Lists</em></span>. Using 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   706
        <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
   707
        <span class="emphasis"><em>Lists</em></span> from other type of sequential data structures or other 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   708
        <span class="emphasis"><em>Lists</em></span> itself. The syntax of 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   709
        <span class="emphasis"><em>List Comprehensions</em></span> consists of a square brackets to indicate the result is a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   710
        <span class="emphasis"><em>List</em></span> within which we include at least one 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   711
        <span class="strong"><strong>for</strong></span> clause and multiple 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   712
        <span class="strong"><strong>if</strong></span> clauses. It will be more clear with an example:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   713
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   714
<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
   715
&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
   716
&gt;&gt;&gt; sq
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   717
[1, 4, 9]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   718
&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
   719
&gt;&gt;&gt; even = [x for x in all_num if x%2 == 0]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   720
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   721
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   722
<p><a name="x_769"></a>The syntax used here is very clear from the way it is written. It can be translated into english as, "for each element x in the list all_num, 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
   723
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   724
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   725
</div></body>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   726
</html>