web/html/ch6oop.html
author amit@thunder
Mon, 25 Jan 2010 18:56:45 +0530
changeset 0 8083d21c0020
child 1 672eaaab9204
permissions -rw-r--r--
The first commit of all the required files for the review app
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     1
<html>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     2
<head>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     3
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     4
<title>Chapter 6. OOP</title>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     5
<link rel="stylesheet" href="/review/support/styles.css" type="text/css">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     6
<meta name="generator" content="DocBook XSL Stylesheets V1.74.3">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     7
<link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     8
<script type="text/javascript" src="/review/support/jquery-min.js"></script>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     9
<script type="text/javascript" src="/review/support/form.js"></script>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    10
<script type="text/javascript" src="/review/support/hsbook.js"></script>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    11
<meta name="generator" content="DocBook XSL Stylesheets V1.75.1">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    12
</head>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    13
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" id="ch6oop">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    14
<div class="titlepage"></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    15
<div class="toc">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    16
<p><b>Table of Contents</b></p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    17
<dl><dt><span class="article"><a href="#id2582702">Classes and Objects</a></span></dt></dl>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    18
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    19
<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
    20
<div class="titlepage">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    21
<div><div><h2 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    22
<a name="id2582702"></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
    23
<hr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    24
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    25
<p id="ch6oop_1"></a>In the previous sections we learnt about functions which provide certain level
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    26
of abstraction to our code by holding the code which performs one or more
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    27
specific functionalities. We were able to use this function as many times as we
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    28
wanted. In addition to functions, Python also higher level of abstractions
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    29
through <span class="emphasis"><em>Classes</em></span> and <span class="emphasis"><em>Objects</em></span>. <span class="emphasis"><em>Objects</em></span> can be loosely defined as a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    30
collection of a set of data items and a set of methods. The data items can be
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    31
any valid Python variable or any Python object. Functions enclosed within a class
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    32
are called as <span class="emphasis"><em>methods</em></span>. If you are thinking if methods are functions why is there
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    33
a distinction between the two? The answer to this will be given as we walk through
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    34
the concepts of <span class="emphasis"><em>Classes</em></span> and <span class="emphasis"><em>Objects</em></span>. <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
    35
<span class="emphasis"><em>Objects</em></span>. <span class="emphasis"><em>Objects</em></span> are instances of <span class="emphasis"><em>Classes</em></span>.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    36
<p id="ch6oop_2"></a>A class is defined using the keyword <span class="strong"><strong>class</strong></span> followed by the class name, in
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    37
turn followed by a semicolon. The statements that a <span class="emphasis"><em>Class</em></span> encloses are written
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    38
in a new block, i.e on the next indentation level:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    39
<pre class="programlisting"> class Employee:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    40
  def setName(self, name):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    41
    self.name = name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    42
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    43
  def getName(self):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    44
    return self.name</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    45
<p id="ch6oop_3"></a>In the above example, we defined a class with the name Employee. We also defined
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    46
two methods, setName and getName for this class. It is important to note the
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    47
differences between the normal Python functions and class methods defined above.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    48
Each method of the class must take the same instance of the class(object) from
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    49
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
    50
<span class="emphasis"><em>self</em></span>. Note that <span class="emphasis"><em>self</em></span> is only a convention. You can use any other name, but
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    51
the first argument to the method will always be the same object of the class
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    52
from which the method was called. The data memebers that belong to the class are
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    53
called as <span class="emphasis"><em>class attributes</em></span>. <span class="emphasis"><em>Class attributes</em></span> are preceded by the object of
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    54
the class and a dot. In the above example, <span class="emphasis"><em>name</em></span> is a class attribute since it
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    55
is preceded by the <span class="emphasis"><em>self</em></span> object. <span class="emphasis"><em>Class attributes</em></span> can be accessed from
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    56
anywhere within the class.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    57
<p id="ch6oop_4"></a>We can create objects of a class outside the class definition by using the same
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    58
syntax we use to call a function with no parameters. We can assign this object
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    59
to a variable:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    60
<pre class="programlisting"> emp = Employee()</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    61
<p id="ch6oop_5"></a>In the above example, we create an object named <span class="emphasis"><em>emp</em></span> of the class <span class="emphasis"><em>Employee</em></span>.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    62
All the attributes and methods of the class can be accessed by the object of the
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    63
class using the standard notation <span class="emphasis"><em>object.attribute</em></span> or <span class="emphasis"><em>object.method()</em></span>.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    64
Although the first parameter of a class method is the self object, it must not
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    65
be passed as an argument when calling the method. The <span class="emphasis"><em>self</em></span> object is implicitly
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    66
passed to the method by the Python interpreter. All other arguments passing rules
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    67
like default arguments, keyword arguments, argument packing and unpacking follow
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    68
the same rules as those for ordinary Python functions:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    69
<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
    70
&gt;&gt;&gt; name = emp.getName()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    71
&gt;&gt;&gt; print name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    72
John
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    73
&gt;&gt;&gt; print emp.name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    74
John</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    75
<p id="ch6oop_6"></a>If we at all try to access a class attribute before assigning a value to it, i.e
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    76
before creating it, Python raises the same error as it would raise for the
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    77
accessing undefined variable:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    78
<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
    79
&gt;&gt;&gt; emp.name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    80
Traceback (most recent call last):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    81
  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
    82
    print e.name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    83
AttributeError: Employee instance has no attribute 'name'</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    84
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    85
</div></body>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    86
</html>