web/html/backup/ch03-oop.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>
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>Classes and Objects</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
<link rel="home" href="index.html" title="Chapter 1. Classes and Object">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     8
<link rel="up" href="index.html" title="Chapter 1. Classes and Object">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     9
<link rel="prev" href="index.html" title="Chapter 1. Classes and Object">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    10
</head>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    11
<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
    12
<div class="navheader">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    13
<table width="100%" summary="Navigation header">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    14
<tr><th colspan="3" align="center">Classes and Objects</th></tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    15
<tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    16
<td width="20%" align="left">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    17
<a accesskey="p" href="index.html">Prev</a> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    18
<th width="60%" align="center"> </th>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    19
<td width="20%" align="right"> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    20
</tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    21
</table>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    22
<hr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    23
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    24
<div class="article" title="Classes and Objects">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    25
<div class="titlepage">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    26
<div><div><h2 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    27
<a name="id2898687"></a>Classes and Objects</h2></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    28
<hr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    29
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    30
<p><a name="x_9e"></a>In the previous sections we learnt about functions which provide certain level of abstraction to our code by holding the code which performs one or more specific functionalities. We were able to use this function as many times as we wanted. In addition to functions, Python also higher level of abstractions through 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    31
    <span class="emphasis"><em>Classes</em></span> and 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    32
    <span class="emphasis"><em>Objects</em></span>. 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    33
    <span class="emphasis"><em>Objects</em></span> can be loosely defined as a collection of a set of data items and a set of methods. The data items can be any valid Python variable or any Python object. Functions enclosed within a class are called as 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    34
    <span class="emphasis"><em>methods</em></span>. If you are thinking if methods are functions why is there a distinction between the two? The answer to this will be given as we walk through the concepts of 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    35
    <span class="emphasis"><em>Classes</em></span> and 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    36
    <span class="emphasis"><em>Objects</em></span>. 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    37
    <span class="emphasis"><em>Classes</em></span> contain the definition for the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    38
    <span class="emphasis"><em>Objects</em></span>. 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    39
    <span class="emphasis"><em>Objects</em></span> are instances of 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    40
    <span class="emphasis"><em>Classes</em></span>.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    41
  </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    42
<p><a name="x_9f"></a>A class is defined using the keyword 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    43
    <span class="strong"><strong>class</strong></span> followed by the class name, in turn followed by a semicolon. The statements that a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    44
    <span class="emphasis"><em>Class</em></span> encloses are written in a new block, i.e on the next indentation level:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    45
  </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    46
<pre class="programlisting">class Employee:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    47
  def setName(self, name):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    48
    self.name = name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    49
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    50
  def getName(self):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    51
    return self.name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    52
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    53
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    54
<p><a name="x_a0"></a>In the above example, we defined a class with the name Employee. We also defined two methods, setName and getName for this class. It is important to note the differences between the normal Python functions and class methods defined above. Each method of the class must take the same instance of the class(object) from which it was called as the first argument. It is conventionally given the name, 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    55
    <span class="emphasis"><em>self</em></span>. Note that 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    56
    <span class="emphasis"><em>self</em></span> is only a convention. You can use any other name, but the first argument to the method will always be the same object of the class from which the method was called. The data memebers that belong to the class are called as 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    57
    <span class="emphasis"><em>class attributes</em></span>. 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    58
    <span class="emphasis"><em>Class attributes</em></span> are preceded by the object of the class and a dot. In the above example, 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    59
    <span class="emphasis"><em>name</em></span> is a class attribute since it is preceded by the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    60
    <span class="emphasis"><em>self</em></span> object. 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    61
    <span class="emphasis"><em>Class attributes</em></span> can be accessed from anywhere within the class.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    62
  </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    63
<p><a name="x_a1"></a>We can create objects of a class outside the class definition by using the same syntax we use to call a function with no parameters. We can assign this object to a variable:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    64
<pre class="programlisting">emp = Employee()
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_a2"></a>In the above example, we create an object named 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    68
    <span class="emphasis"><em>emp</em></span> of the class 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    69
    <span class="emphasis"><em>Employee</em></span>. All the attributes and methods of the class can be accessed by the object of the class using the standard notation 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    70
    <span class="emphasis"><em>object.attribute</em></span> or 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    71
    <span class="emphasis"><em>object.method()</em></span>. Although the first parameter of a class method is the self object, it must not be passed as an argument when calling the method. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    72
    <span class="emphasis"><em>self</em></span> object is implicitly passed to the method by the Python interpreter. All other arguments passing rules like default arguments, keyword arguments, argument packing and unpacking follow the same rules as those for ordinary Python functions:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    73
  </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    74
<pre class="programlisting">&gt;&gt;&gt; emp.setName('John')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    75
&gt;&gt;&gt; name = emp.getName()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    76
&gt;&gt;&gt; print name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    77
John
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    78
&gt;&gt;&gt; print emp.name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    79
John
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    80
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    81
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    82
<p><a name="x_a3"></a>If we at all try to access a class attribute before assigning a value to it, i.e before creating it, Python raises the same error as it would raise for the accessing undefined variable:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    83
<pre class="programlisting">&gt;&gt;&gt; emp = Employee()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    84
&gt;&gt;&gt; emp.name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    85
Traceback (most recent call last):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    86
  File "class.py", line 10, in &lt;module&gt;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    87
    print e.name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    88
AttributeError: Employee instance has no attribute 'name'
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
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    92
<div class="navfooter">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    93
<hr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    94
<table width="100%" summary="Navigation footer">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    95
<tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    96
<td width="40%" align="left">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    97
<a accesskey="p" href="index.html">Prev</a> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    98
<td width="20%" align="center"> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    99
<td width="40%" align="right"> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   100
</tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   101
<tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   102
<td width="40%" align="left" valign="top">Chapter 1. Classes and Object </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   103
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   104
<td width="40%" align="right" valign="top"> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   105
</tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   106
</table>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   107
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   108
</body>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   109
</html>