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