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