1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
{
"metadata": {
"name": "Chapter 12"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": "<h1>Chapter 12: A Closer Look at Classes<h1>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 12.1, Page Number: 274<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Implementation of friend functions in python'''\n\nclass myclass:\n __a=None\n __b=None\n def __init__(self,i,j):\n self.__a=i\n self.__b=j\n def sum(self,x): #Friend function\n return sum1(x)\n \ndef sum1(x): \n return x._myclass__a +x._myclass__b #accessing private members\n\n#Variable declaration\nn=myclass(3,4)\n\n#Result\nprint n.sum(n)\n\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "7\n"
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 12.2, Page Number: 275<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Use a friend function'''\n\nclass c1:\n __status=None\n def set_status(self,state):\n self.__status=state\n \nclass c2:\n __status=None\n def set_status(self,state):\n self.status=state\n \n#Friend function \ndef idle(a,b):\n if a._c1__status or b._c2__status :\n return 0\n else:\n return 1\n \n#variable declarations\ndef IDLE(): #Constants\n return 0\ndef INUSE():\n return 1\nx=c1()\ny=c2()\n\nx.set_status(IDLE())\ny.set_status(IDLE())\n\nif idle(x,y):\n print \"Screen Can Be Used.\"\n \nx.set_status(INUSE())\n\nif idle(x,y):\n print \"Screen Can Be Used.\"\nelse:\n print \"Pop-up In Use.\"\n \n ",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Screen Can Be Used.\nPop-up In Use.\n"
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 12.3, Page Number: 277<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''A function can be member of one class and a friend of another'''\n\n #Constants\ndef IDLE(): \n return 0\ndef INUSE():\n return 1\n\nclass c1:\n __status=None\n def set_status(self,state):\n self.__status=state\n def idle(self,b): #now a member of c1\n if self.__status or b._c2__status :\n return 0\n else:\n return 1\n \nclass c2:\n __status=None #IDLE if off INUSE if on screen\n def set_status(self,state):\n self.status=state\n \n#Variable declarations \nx=c1()\ny=c2()\n\nx.set_status(IDLE())\ny.set_status(IDLE())\n\nif idle(x,y):\n print \"Screen Can Be Used.\"\n \nx.set_status(INUSE())\n\nif idle(x,y):\n print \"Screen Can Be Used.\"\nelse:\n print \"Pop-up In Use.\"\n ",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Screen Can Be Used.\nPop-up In Use.\n"
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 12.4, Page Number: 278<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Use of overloaded constructors'''\n#Printing the time passed since the functon started instead of ringing the bell.\n\nimport time,string\n\nclass timer:\n __seconds=None\n \n def __init__(self,t1,t2=None):\n if t2==None:\n if isinstance(t1,int): #seconds specified as an integer\n self.__seconds=t1\n else: #seconds specified as a string\n self.__seconds=string.atoi(t1)\n else: #time in minutes and seconds\n self.__seconds=t1*60+t2\n \n def run(self):\n t1=time.clock()\n while (time.clock()-t1)<self.__seconds:\n a=10\n print time.clock()-t1\n \n \na=timer(10)\nb=timer(\"20\")\nc=timer(1,10)\na.run() #count 10 seconds\nb.run() #count 20 seconds\nc.run() #count 1 minute,10 seconds\n ",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "10.0000009774\n20.0000009774"
},
{
"output_type": "stream",
"stream": "stdout",
"text": "\n70.0000004887"
},
{
"output_type": "stream",
"stream": "stdout",
"text": "\n"
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 12.5, Page Number: 280<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Demonstarate dynamic initialization'''\n#Printing the time passed since the functon started instead of ringing the bell.\n\nimport time,string\n\nclass timer:\n __seconds=None\n \n def __init__(self,t1,t2=None):\n if t2==None:\n if isinstance(t1,int): #seconds specified as an integer\n self.__seconds=t1\n else: #seconds specified as a string\n self.__seconds=string.atoi(t1)\n else: #time in minutes and seconds\n self.__seconds=t1*60+t2\n \n def run(self):\n t1=time.clock()\n while (time.clock()-t1)<self.__seconds:\n a=10\n print time.clock()-t1\n \na=timer(10)\na.run()\n\nprint \"Enter number of seconds: \"\nstr=\"20\"\nb=timer(str) #initialize at the run time\nc.run()\n\nprint \"Enter minutes and seconds: \"\nmin=1\nsec=10\nc=timer(min,sec) #initialize at the run time\nc.run()\n ",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "10.0000004887\nEnter number of seconds: \n70.0000009774"
},
{
"output_type": "stream",
"stream": "stdout",
"text": "\nEnter minutes and seconds: \n70.0000009774"
},
{
"output_type": "stream",
"stream": "stdout",
"text": "\n"
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 12.6, Page Number: 282<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Demonstrate object assignment'''\n\nclass myclass:\n __a=None #private members\n __b=None\n def setab(self,i,j): #publc functons\n self.__a=i\n self.__b=j\n def showab(self):\n print \"a is \",self.__a\n print \"b is \",self.__b\n\n#Variable declaration\nob1 = myclass()\nob2 = myclass()\n\n#Intalizing\nob1.setab(10,20)\nob2.setab(0,0)\n\nprint \"ob1 before assignment: \"\nob1.showab()\nprint \"ob2 before assignment: \"\nob2.showab()\n\nob2 = ob1 #assign ob1 to ob2\n\n#Result\nprint \"ob1 after assignment: \"\nob1.showab()\nprint \"ob2 after assignment: \"\nob2.showab()\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "ob1 before assignment: \na is 10\nb is 20\nob2 before assignment: \na is 0\nb is 0\nob1 after assignment: \na is 10\nb is 20\nob2 after assignment: \na is 10\nb is 20\n"
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 12.7, Page Number: 283<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Demonstration of passing objects to functions'''\n'''Implementing call by value method in python'''\n\nfrom copy import deepcopy\n \nclass OBJ:\n def set_i(self,x):\n self.__i=x\n def out_i(self):\n print self.__i,\n \ndef f(x):\n x=deepcopy(x)\n x.out_i() #outputs 10\n x.set_i(100) #this affects only local copy\n x.out_i() #outputs 100\n \n#Variable declaration\no=OBJ()\no.set_i(10)\nf(o) \no.out_i() #still outputs 10, value of i unchanged\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "10 100 10\n"
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 12.8, Page Number: 284<h3> "
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Constructors, destructors, and passing objects'''\n\nclass myclass: \n def __init__(self,i):\n self.__val=i\n print \"Constructing\"\n def __del__(self):\n print \"Destructing\"\n def getval(self):\n return self.__val\n \ndef display(ob):\n print ob.getval()\n\n#Varable declaration\na=myclass(10)\n\ndisplay(a)\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Constructing\nDestructing\n10\n"
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 12.9, Page Number: 286<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Passing objects with a pointer'''\n#The problem shown in C++, will not occur in python because it does not have pointers.\n\nfrom ctypes import *\n\nclass myclass:\n def __init__(self,i):\n print \"Allocating p\"\n self.p=pointer(c_int(i))\n def __del__(self):\n print \"Freeing p\"\n def getval(self):\n return self.p[0]\n \ndef display(ob):\n print ob.getval()\n\n#Variable declaration\na=myclass(10)\n\ndisplay(a)\n ",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Allocating p\n10\n"
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 12.10, Page Number: 287<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Passing objects by reference'''\n\nfrom ctypes import *\nclass myclass:\n def __init__(self,i):\n print \"Allocating p\"\n self.p=pointer(c_int(i))\n def __del__(self):\n print \"Freeing p\"\n def getval(self):\n return self.p[0]\n \ndef display(ob):\n print ob[0].getval()\n\n#Variable declaration\na=[]\na.append(myclass(10))\n\ndisplay(a)\n ",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Freeing p\nAllocating p\n10\n"
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 12.11, Page Number: 288<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Returning an object'''\n\nclass sample:\n __s=None\n def show(self):\n print self.__s\n def set(self,str):\n self.__s=str\n\n#Return an object of type sample\ndef input():\n str=sample()\n instr = \"Hello\" #User input\n str.set(instr)\n return str\n\n#Variable declaration\nob=sample()\n\n#assign returned object to ob\nob=input()\n\n#Result\nob.show()\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Hello\n"
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 12.12, Page Number: 289<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Returning an object'''\n#The error shown in C++ doesnt occur here.\nclass sample:\n __s=None\n def __init__(self):\n self.__s=0\n def __del__(self): \n print \"Freeing p\"\n def show(self):\n print self.__s\n def set(self,str):\n self.__s=str\n \n#This function takes one object parameter\ndef input():\n str=sample()\n instr=\"Hello\" #User input\n str.set(instr)\n return str\n\n#Variable declaration\nob=sample()\n\n#assign returned object to ob\nob=input()\n\n#Result\nob.show()\n\n\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Freeing p\nFreeing p\nHello\n"
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 12.13, Page Number: 292<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Implementing a copy constructor'''\n#Copy construcor doesnt work in this example, it works only when explicitly called\n\nclass myclass:\n __p=None\n def __init__(self,i):\n if isinstance(i,int):\n print \"Allocating p\"\n self.__p=i\n else:\n print \"Copy constructor called\"\n self.__p=i.getval()\n def __del__(self): \n print \"Freeing p\"\n def getval(self):\n return self.__p\n \n#This function takes one object parameter\ndef display(ob):\n print ob.getval()\n\n#Variable declaration\nob=myclass(10)\n\n#Result\ndisplay(ob)\n\n\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Allocating p\n10\n"
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 12.14, Page Number: 294<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Implementing a copy constructor'''\n\n\nclass myclass:\n __p=None\n def __init__(self,i):\n if isinstance(i,int):\n print \"Allocating p\"\n self.__p=i\n else:\n print \"Copy constructor called\"\n self.__p=i.getval()\n def __del__(self): \n print \"Freeing p\"\n def getval(self):\n return self.__p\n \n\n#Variable declaration\na=myclass(10) #calls normal constructor\nb=myclass(a) #calls copy constructor\n\n\n\n\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Allocating p\nCopy constructor called\n"
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 12.15, Page Number: 295<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Implementing a copy constructor'''\n\n\nclass myclass:\n def __init__(self,i=0):\n if isinstance(i,int):\n print \"Normal constructor\"\n else:\n print \"Copy constructor\"\n\n\n#Variable declaration\na=myclass() #calls normal constructor\n\nf=myclass()\na=myclass(f) #Invoke copyconstructor\n\n\n\n\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Normal constructor\nNormal constructor\nCopy constructor\n"
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3>Example 12.16, Page Number: 297<h3>"
},
{
"cell_type": "code",
"collapsed": false,
"input": "'''Implementation of the this pointer'''\n#Here self works as this\n\nclass c1:\n def __init__(self):\n self.__i=None\n def load_i(self,val):\n self.__i=val\n def get_i(self):\n return self.__i\n \n#Variable declaration \no=c1()\n\no.load_i(100)\n\n#Result\nprint o.get_i()\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "100\n"
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
|