author | amit@thunder |
Thu, 25 Feb 2010 00:04:27 +0530 | |
changeset 27 | cb14131583c6 |
parent 25 | ed38dd9bdb50 |
child 28 | 514098969b11 |
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> |
6 | 2 |
<head> |
3 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
|
4 |
<title> |
|
5 |
Chapter. oop |
|
6 |
</title> |
|
15 | 7 |
<link rel="stylesheet" href="/review/support/styles.css" type="text/css" /> |
8 |
<meta name="generator" content="DocBook XSL Stylesheets V1.74.3" /> |
|
9 |
<link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" /> |
|
10 |
<script type="text/javascript" src="/review/support/jquery-min.js"> |
|
11 |
</script> |
|
12 |
<script type="text/javascript" src="/review/support/form.js"> |
|
13 |
</script> |
|
14 |
<script type="text/javascript" src="/review/support/hsbook.js"> |
|
15 |
</script> |
|
6 | 16 |
</head> |
17 |
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
|
16 | 18 |
<div class="chapter" id="ch6oop"> |
6 | 19 |
<div class="titlepage"> |
20 |
</div> |
|
21 |
<div class="toc"> |
|
22 |
<p> |
|
23 |
<b> |
|
24 |
Table of Contents |
|
25 |
</b> |
|
26 |
</p> |
|
27 |
<dl> |
|
28 |
<dt> |
|
29 |
<span class="article"> |
|
27
cb14131583c6
Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
25
diff
changeset
|
30 |
<a href="#id2471018"> |
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 |
Classes and Objects |
6 | 32 |
</a> |
33 |
</span> |
|
34 |
</dt> |
|
35 |
</dl> |
|
36 |
</div> |
|
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 |
<div class="article" title="Classes and Objects"> |
6 | 38 |
<div class="titlepage"> |
39 |
<div> |
|
40 |
<div> |
|
24
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
41 |
<h2 class="title"> |
27
cb14131583c6
Started on final changes to the build scripts so that we can start with totally automatic builds
amit@thunder
parents:
25
diff
changeset
|
42 |
<a name="id2471018"> |
24
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
43 |
</a> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
44 |
Classes and Objects |
6 | 45 |
</h2> |
46 |
</div> |
|
47 |
</div> |
|
48 |
<hr /> |
|
49 |
</div> |
|
24
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
50 |
<p id="ch6oop_1"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
51 |
In the previous sections we learnt about functions which provide certain level |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
52 |
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
|
53 |
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
|
54 |
wanted. In addition to functions, Python also higher level of abstractions |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
55 |
through |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
56 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
57 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
58 |
Classes |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
59 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
60 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
61 |
and |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
62 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
63 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
64 |
Objects |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
65 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
66 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
67 |
. |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
68 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
69 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
70 |
Objects |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
71 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
72 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
73 |
can be loosely defined as a |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
74 |
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
|
75 |
any valid Python variable or any Python object. Functions enclosed within a class |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
76 |
are called as |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
77 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
78 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
79 |
methods |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
80 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
81 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
82 |
. If you are thinking if methods are functions why is there |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
83 |
a distinction between the two? The answer to this will be given as we walk through |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
84 |
the concepts of |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
85 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
86 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
87 |
Classes |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
88 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
89 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
90 |
and |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
91 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
92 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
93 |
Objects |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
94 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
95 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
96 |
. |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
97 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
98 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
99 |
Classes |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
100 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
101 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
102 |
contain the definition for the |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
103 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
104 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
105 |
Objects |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
106 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
107 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
108 |
. |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
109 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
110 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
111 |
Objects |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
112 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
113 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
114 |
are instances of |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
115 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
116 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
117 |
Classes |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
118 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
119 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
120 |
. |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
121 |
</p> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
122 |
<p id="ch6oop_2"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
123 |
A class is defined using the keyword |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
124 |
<span class="strong"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
125 |
<strong> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
126 |
class |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
127 |
</strong> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
128 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
129 |
followed by the class name, in |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
130 |
turn followed by a semicolon. The statements that a |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
131 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
132 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
133 |
Class |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
134 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
135 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
136 |
encloses are written |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
137 |
in a new block, i.e on the next indentation level: |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
138 |
</p> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
139 |
<pre class="programlisting"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
140 |
class Employee: |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
141 |
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
|
142 |
self.name = name |
0
8083d21c0020
The first commit of all the required files for the review app
amit@thunder
parents:
diff
changeset
|
143 |
|
24
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
144 |
def getName(self): |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
145 |
return 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
|
146 |
</pre> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
147 |
<p id="ch6oop_3"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
148 |
In the above example, we defined a class with the name Employee. We also defined |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
149 |
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
|
150 |
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
|
151 |
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
|
152 |
which it was called as the first argument. It is conventionally given the name, |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
153 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
154 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
155 |
self |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
156 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
157 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
158 |
. Note that |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
159 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
160 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
161 |
self |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
162 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
163 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
164 |
is only a convention. You can use any other name, but |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
165 |
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
|
166 |
from which the method was called. The data memebers that belong to the class are |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
167 |
called as |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
168 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
169 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
170 |
class attributes |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
171 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
172 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
173 |
. |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
174 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
175 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
176 |
Class attributes |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
177 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
178 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
179 |
are preceded by the object of |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
180 |
the class and a dot. In the above example, |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
181 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
182 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
183 |
name |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
184 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
185 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
186 |
is a class attribute since it |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
187 |
is preceded by the |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
188 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
189 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
190 |
self |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
191 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
192 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
193 |
object. |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
194 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
195 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
196 |
Class attributes |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
197 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
198 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
199 |
can be accessed from |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
200 |
anywhere within 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
|
201 |
</p> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
202 |
<p id="ch6oop_4"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
203 |
We can create objects of a class outside the class definition by using the same |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
204 |
syntax we use to call a function with no parameters. We can assign this object |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
205 |
to a variable: |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
206 |
</p> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
207 |
<pre class="programlisting"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
208 |
emp = Employee() |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
209 |
</pre> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
210 |
<p id="ch6oop_5"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
211 |
In the above example, we create an object named |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
212 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
213 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
214 |
emp |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
215 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
216 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
217 |
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
|
218 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
219 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
220 |
Employee |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
221 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
222 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
223 |
. |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
224 |
All the attributes and methods of the class can be accessed by the object of the |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
225 |
class using the standard notation |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
226 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
227 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
228 |
object.attribute |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
229 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
230 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
231 |
or |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
232 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
233 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
234 |
object.method() |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
235 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
236 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
237 |
. |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
238 |
Although the first parameter of a class method is the self object, it must not |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
239 |
be passed as an argument when calling the method. The |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
240 |
<span class="emphasis"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
241 |
<em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
242 |
self |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
243 |
</em> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
244 |
</span> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
245 |
object is implicitly |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
246 |
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
|
247 |
like default arguments, keyword arguments, argument packing and unpacking follow |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
248 |
the same rules as those for ordinary Python functions: |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
249 |
</p> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
250 |
<pre class="programlisting"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
251 |
>>> emp.setName('John') |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
252 |
>>> 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
|
253 |
>>> 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
|
254 |
John |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
255 |
>>> print 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
|
256 |
John |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
257 |
</pre> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
258 |
<p id="ch6oop_6"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
259 |
If we at all try to access a class attribute before assigning a value to it, i.e |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
260 |
before creating it, Python raises the same error as it would raise for the |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
261 |
accessing undefined variable: |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
262 |
</p> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
263 |
<pre class="programlisting"> |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
264 |
>>> emp = Employee() |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
265 |
>>> 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
|
266 |
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
|
267 |
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
|
268 |
print e.name |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
269 |
AttributeError: Employee instance has no attribute 'name' |
10074d1357ff
Some changes after the horrible problem of writing the same ch9 on all the html pages
amit@thunder
parents:
16
diff
changeset
|
270 |
</pre> |
6 | 271 |
</div> |
272 |
</div> |
|
273 |
</body> |
|
0
8083d21c0020
The first commit of all the required files for the review app
amit@thunder
parents:
diff
changeset
|
274 |
</html> |