-
Notifications
You must be signed in to change notification settings - Fork 10
/
oop.html
executable file
·1041 lines (881 loc) · 43 KB
/
oop.html
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>13. More Classes and Objects — How to Think Like a Computer Scientist: Learning with Python 3 (AoPS Edition)</title>
<link rel="stylesheet" href="_static/style.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/codemirrorEdited.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" src="_static/pywindowCodemirrorC.js"></script>
<script type="text/javascript" src="_static/skulpt.min.js"></script>
<script type="text/javascript" src="_static/skulpt-stdlib.js"></script>
<script type="text/javascript" src="_static/aopsmods.js"></script>
<link rel="copyright" title="Copyright" href="copyright.html" />
<link rel="top" title="How to Think Like a Computer Scientist: Learning with Python 3 (AoPS Edition)" href="index.html" />
<link rel="next" title="14. Collections of Objects" href="collections.html" />
<link rel="prev" title="12. Classes and Objects — the Basics" href="classes.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="collections.html" title="14. Collections of Objects"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="classes.html" title="12. Classes and Objects — the Basics"
accesskey="P">previous</a> |</li>
<li><a href="index.html">How to Think Like a Computer Scientist: Learning with Python 3 (AoPS Edition)</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="body">
<div class="line-block">
<div class="line"><br /></div>
</div>
<div class="section" id="more-classes-and-objects">
<h1>13. More Classes and Objects<a class="headerlink" href="#more-classes-and-objects" title="Permalink to this headline">¶</a></h1>
<div class="section" id="mytime">
<h2>13.1. MyTime<a class="headerlink" href="#mytime" title="Permalink to this headline">¶</a></h2>
<p>As another example of a user-defined type, we’ll define a class called <tt class="docutils literal"><span class="pre">MyTime</span></tt>
that records the time of day. We’ll provide an <tt class="docutils literal"><span class="pre">__init__</span></tt> method to ensure
that every instance is created with appropriate attributes and initialization.
The class definition looks like this:</p>
<div id="mytimedef" class="pywindow" >
<div id="mytimedef_code_div" style="display: block">
<textarea rows="10" id="mytimedef_code" class="active_code" prefixcode="undefined">
class MyTime:
def __init__(self, hrs=0, mins=0, secs=0):
""" Create a MyTime object initialized to hrs, mins, secs """
self.hours = hrs
self.minutes = mins
self.seconds = secs
# create a new MyTime object
tim1 = MyTime(11, 59, 30)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['mytimedef_code'] = true;
pythonTool.readOnlyFlags['mytimedef_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="mytimedef_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="mytimedef_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="mytimedef_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='mytimedef_error'></div>
<div style="text-align: center">
<canvas id="mytimedef_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="mytimedef_suffix" style="display:none">
</pre>
<pre id="mytimedef_pre" class="active_out">
</pre>
<div id="mytimedef_files" class="ac-files ac-files-hidden"></div>
</div>
<p>The state diagram for the object looks like this:</p>
<blockquote>
<div><img alt="_images/time.png" src="_images/time.png" />
</div></blockquote>
<p>We should also add a <tt class="docutils literal"><span class="pre">__str__</span></tt>
method so that MyTime objects can print themselves decently.</p>
<div id="mytimestr" class="pywindow" >
<div id="mytimestr_code_div" style="display: block">
<textarea rows="24" id="mytimestr_code" class="active_code" prefixcode="undefined">
class MyTime:
def __init__(self, hrs=0, mins=0, secs=0):
""" Create a MyTime object initialized to hrs, mins, secs """
self.hours = hrs
self.minutes = mins
self.seconds = secs
def __str__(self):
timeString = ""
if self.hours < 10:
timeString += "0"
timeString += str(self.hours) + ":"
if self.minutes < 10:
timeString += "0"
timeString += str(self.minutes) + ":"
if self.seconds < 10:
timeString += "0"
timeString += str(self.seconds)
return timeString
# create a new MyTime object
tim1 = MyTime(11, 59, 30)
print(tim1)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['mytimestr_code'] = true;
pythonTool.readOnlyFlags['mytimestr_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="mytimestr_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="mytimestr_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="mytimestr_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='mytimestr_error'></div>
<div style="text-align: center">
<canvas id="mytimestr_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="mytimestr_suffix" style="display:none">
</pre>
<pre id="mytimestr_pre" class="active_out">
</pre>
<div id="mytimestr_files" class="ac-files ac-files-hidden"></div>
</div>
</div>
<div class="section" id="pure-functions">
<span id="index-0"></span><h2>13.2. Pure functions<a class="headerlink" href="#pure-functions" title="Permalink to this headline">¶</a></h2>
<p>In the next few sections, we’ll write two versions of a function called
<tt class="docutils literal"><span class="pre">add_time</span></tt>, which calculates the sum of two <tt class="docutils literal"><span class="pre">MyTime</span></tt> objects. They will demonstrate
two kinds of functions: pure functions and modifiers.</p>
<p>The following is a rough version of <tt class="docutils literal"><span class="pre">add_time</span></tt>:</p>
<div id="addtimebad" class="pywindow" >
<div id="addtimebad_code_div" style="display: block">
<textarea rows="27" id="addtimebad_code" class="active_code" prefixcode="undefined">
class MyTime:
def __init__(self, hrs=0, mins=0, secs=0):
""" Create a MyTime object initialized to hrs, mins, secs """
self.hours = hrs
self.minutes = mins
self.seconds = secs
def __str__(self):
timeString = ""
if self.hours < 10:
timeString += "0"
timeString += str(self.hours) + ":"
if self.minutes < 10:
timeString += "0"
timeString += str(self.minutes) + ":"
if self.seconds < 10:
timeString += "0"
timeString += str(self.seconds)
return timeString
def add_time(t1, t2):
h = t1.hours + t2.hours
m = t1.minutes + t2.minutes
s = t1.seconds + t2.seconds
sumTime = MyTime(h, m, s)
return sumTime</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['addtimebad_code'] = true;
pythonTool.readOnlyFlags['addtimebad_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="addtimebad_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="addtimebad_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="addtimebad_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='addtimebad_error'></div>
<div style="text-align: center">
<canvas id="addtimebad_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="addtimebad_suffix" style="display:none">
</pre>
<pre id="addtimebad_pre" class="active_out">
</pre>
<div id="addtimebad_files" class="ac-files ac-files-hidden"></div>
</div>
<p>The function creates a new <tt class="docutils literal"><span class="pre">MyTime</span></tt> object and
returns a reference to the new object. This is called a <strong>pure function</strong>
because it does not modify any of the objects passed to it as parameters and it
has no side effects, such as updating global variables,
displaying a value, or getting user input.</p>
<p>Here is an example of how to use this function. We’ll create two <tt class="docutils literal"><span class="pre">MyTime</span></tt>
objects: <tt class="docutils literal"><span class="pre">currentTime</span></tt>, which contains the current time; and <tt class="docutils literal"><span class="pre">breadTime</span></tt>,
which contains the amount of time it takes for a breadmaker to make bread. Then
we’ll use <tt class="docutils literal"><span class="pre">add_time</span></tt> to figure out when the bread will be done.</p>
<div id="addtimebadex" class="pywindow" >
<div id="addtimebadex_code_div" style="display: block">
<textarea rows="32" id="addtimebadex_code" class="active_code" prefixcode="undefined">
class MyTime:
def __init__(self, hrs=0, mins=0, secs=0):
""" Create a MyTime object initialized to hrs, mins, secs """
self.hours = hrs
self.minutes = mins
self.seconds = secs
def __str__(self):
timeString = ""
if self.hours < 10:
timeString += "0"
timeString += str(self.hours) + ":"
if self.minutes < 10:
timeString += "0"
timeString += str(self.minutes) + ":"
if self.seconds < 10:
timeString += "0"
timeString += str(self.seconds)
return timeString
def add_time(t1, t2):
h = t1.hours + t2.hours
m = t1.minutes + t2.minutes
s = t1.seconds + t2.seconds
sumTime = MyTime(h, m, s)
return sumTime
currentTime = MyTime(9, 14, 30)
breadTime = MyTime(3, 35, 0)
doneTime = add_time(currentTime, breadTime)
print(doneTime)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['addtimebadex_code'] = true;
pythonTool.readOnlyFlags['addtimebadex_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="addtimebadex_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="addtimebadex_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="addtimebadex_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='addtimebadex_error'></div>
<div style="text-align: center">
<canvas id="addtimebadex_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="addtimebadex_suffix" style="display:none">
</pre>
<pre id="addtimebadex_pre" class="active_out">
</pre>
<div id="addtimebadex_files" class="ac-files ac-files-hidden"></div>
</div>
<p>The output of this program is <tt class="docutils literal"><span class="pre">12:49:30</span></tt>, which is correct. On the other
hand, there are cases where the result is not correct. Can you think of one?</p>
<div id="addtimebreaks" class="pywindow" >
<div id="addtimebreaks_code_div" style="display: block">
<textarea rows="32" id="addtimebreaks_code" class="active_code" prefixcode="undefined">
class MyTime:
def __init__(self, hrs=0, mins=0, secs=0):
""" Create a MyTime object initialized to hrs, mins, secs """
self.hours = hrs
self.minutes = mins
self.seconds = secs
def __str__(self):
timeString = ""
if self.hours < 10:
timeString += "0"
timeString += str(self.hours) + ":"
if self.minutes < 10:
timeString += "0"
timeString += str(self.minutes) + ":"
if self.seconds < 10:
timeString += "0"
timeString += str(self.seconds)
return timeString
def add_time(t1, t2):
h = t1.hours + t2.hours
m = t1.minutes + t2.minutes
s = t1.seconds + t2.seconds
sumTime = MyTime(h, m, s)
return sumTime
currentTime = MyTime(6, 32, 40)
breadTime = MyTime(3, 35, 50)
doneTime = add_time(currentTime, breadTime)
print(doneTime)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['addtimebreaks_code'] = true;
pythonTool.readOnlyFlags['addtimebreaks_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="addtimebreaks_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="addtimebreaks_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="addtimebreaks_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='addtimebreaks_error'></div>
<div style="text-align: center">
<canvas id="addtimebreaks_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="addtimebreaks_suffix" style="display:none">
</pre>
<pre id="addtimebreaks_pre" class="active_out">
</pre>
<div id="addtimebreaks_files" class="ac-files ac-files-hidden"></div>
</div>
<p>The problem is that this function does not deal with cases where the number of
seconds or minutes adds up to more than sixty. When that happens, we have to
carry the extra seconds into the minutes column or the extra minutes into the
hours column.</p>
<p>Here’s a better version of the function:</p>
<div id="addtimebetter" class="pywindow" >
<div id="addtimebetter_code_div" style="display: block">
<textarea rows="38" id="addtimebetter_code" class="active_code" prefixcode="undefined">
class MyTime:
def __init__(self, hrs=0, mins=0, secs=0):
""" Create a MyTime object initialized to hrs, mins, secs """
self.hours = hrs
self.minutes = mins
self.seconds = secs
def __str__(self):
timeString = ""
if self.hours < 10:
timeString += "0"
timeString += str(self.hours) + ":"
if self.minutes < 10:
timeString += "0"
timeString += str(self.minutes) + ":"
if self.seconds < 10:
timeString += "0"
timeString += str(self.seconds)
return timeString
def add_time(t1, t2):
h = t1.hours + t2.hours
m = t1.minutes + t2.minutes
s = t1.seconds + t2.seconds
if s >= 60:
s -= 60
m += 1
if m >= 60:
m -= 60
h += 1
sumTime = MyTime(h, m, s)
return sumTime
currentTime = MyTime(6, 32, 40)
breadTime = MyTime(3, 35, 50)
doneTime = add_time(currentTime, breadTime)
print(doneTime)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['addtimebetter_code'] = true;
pythonTool.readOnlyFlags['addtimebetter_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="addtimebetter_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="addtimebetter_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="addtimebetter_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='addtimebetter_error'></div>
<div style="text-align: center">
<canvas id="addtimebetter_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="addtimebetter_suffix" style="display:none">
</pre>
<pre id="addtimebetter_pre" class="active_out">
</pre>
<div id="addtimebetter_files" class="ac-files ac-files-hidden"></div>
</div>
<p>This function is starting to get bigger, and still doesn’t work
for all possible cases. Later we will
suggest an alternative approach that yields better code.</p>
</div>
<div class="section" id="modifiers">
<span id="index-1"></span><h2>13.3. Modifiers<a class="headerlink" href="#modifiers" title="Permalink to this headline">¶</a></h2>
<p>There are times when it is useful for a function to modify one or more of the
objects it gets as parameters. Usually, the caller keeps a reference to the
objects it passes, so any changes the function makes are visible to the caller.
Functions that work this way are called <strong>modifiers</strong>.</p>
<p><tt class="docutils literal"><span class="pre">increment</span></tt>, which adds a given number of seconds to a <tt class="docutils literal"><span class="pre">MyTime</span></tt> object, would
be written most naturally as a modifier. A rough draft of the function looks like this:</p>
<div id="incrfirst" class="pywindow" >
<div id="incrfirst_code_div" style="display: block">
<textarea rows="8" id="incrfirst_code" class="active_code" prefixcode="undefined">
def increment(t, secs):
t.seconds += secs
if t.seconds >= 60:
t.seconds -= 60
t.minutes += 1
if t.minutes >= 60:
t.minutes -= 60
t.hours += 1</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['incrfirst_code'] = true;
pythonTool.readOnlyFlags['incrfirst_code'] = true;
</script>
<div id='incrfirst_error'></div>
<pre id="incrfirst_suffix" style="display:none">
</pre>
</div>
<p>The first line performs the basic operation; the remainder deals with the
special cases we saw before.</p>
<p>Is this function correct? What happens if the parameter <tt class="docutils literal"><span class="pre">seconds</span></tt> is much
greater than sixty? In that case, it is not enough to carry once; we have to
keep doing it until <tt class="docutils literal"><span class="pre">seconds</span></tt> is less than sixty. One solution is to replace
the <tt class="docutils literal"><span class="pre">if</span></tt> statements with <tt class="docutils literal"><span class="pre">while</span></tt> statements:</p>
<div id="incrsecond" class="pywindow" >
<div id="incrsecond_code_div" style="display: block">
<textarea rows="8" id="incrsecond_code" class="active_code" prefixcode="undefined">
def increment(t, secs):
t.seconds += secs
while t.seconds >= 60:
t.seconds -= 60
t.minutes += 1
while t.minutes >= 60:
t.minutes -= 60
t.hours += 1</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['incrsecond_code'] = true;
pythonTool.readOnlyFlags['incrsecond_code'] = true;
</script>
<div id='incrsecond_error'></div>
<pre id="incrsecond_suffix" style="display:none">
</pre>
</div>
<p>This function is now correct when seconds is not negative, and when
hours does not exceed 23, but it is not a particularly good solution.</p>
</div>
<div class="section" id="converting-increment-to-a-method">
<h2>13.4. Converting <tt class="docutils literal"><span class="pre">increment</span></tt> to a method<a class="headerlink" href="#converting-increment-to-a-method" title="Permalink to this headline">¶</a></h2>
<p>Once again, OOP programmers would prefer to put functions that work with
<tt class="docutils literal"><span class="pre">MyTime</span></tt> objects into the <tt class="docutils literal"><span class="pre">MyTime</span></tt> class, so let’s convert <tt class="docutils literal"><span class="pre">increment</span></tt>
to a method.</p>
<div id="mytimeincr" class="pywindow" >
<div id="mytimeincr_code_div" style="display: block">
<textarea rows="33" id="mytimeincr_code" class="active_code" prefixcode="undefined">
class MyTime:
def __init__(self, hrs=0, mins=0, secs=0):
""" Create a MyTime object initialized to hrs, mins, secs """
self.hours = hrs
self.minutes = mins
self.seconds = secs
def __str__(self):
timeString = ""
if self.hours < 10:
timeString += "0"
timeString += str(self.hours) + ":"
if self.minutes < 10:
timeString += "0"
timeString += str(self.minutes) + ":"
if self.seconds < 10:
timeString += "0"
timeString += str(self.seconds)
return timeString
def increment(self, seconds):
self.seconds += seconds
while self.seconds >= 60:
self.seconds -= 60
self.minutes += 1
while self.minutes >= 60:
self.minutes -= 60
self.hours += 1
currentTime = MyTime(6,32,40)
currentTime.increment(70)
print(currentTime) # should print 06:33:50</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['mytimeincr_code'] = true;
pythonTool.readOnlyFlags['mytimeincr_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="mytimeincr_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="mytimeincr_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="mytimeincr_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='mytimeincr_error'></div>
<div style="text-align: center">
<canvas id="mytimeincr_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="mytimeincr_suffix" style="display:none">
</pre>
<pre id="mytimeincr_pre" class="active_out">
</pre>
<div id="mytimeincr_files" class="ac-files ac-files-hidden"></div>
</div>
<p>The transformation is purely mechanical — we move the definition into
the class definition and (optionally) change the name of the first parameter to
<tt class="docutils literal"><span class="pre">self</span></tt>, to fit with Python style conventions.</p>
<p>Now we can invoke <tt class="docutils literal"><span class="pre">increment</span></tt> using the syntax for invoking a method, as on line 32.
Again, the object on which the method is invoked gets assigned to the first
parameter, <tt class="docutils literal"><span class="pre">self</span></tt>. The second parameter, <tt class="docutils literal"><span class="pre">seconds</span></tt> gets the value <tt class="docutils literal"><span class="pre">70</span></tt>.</p>
</div>
<div class="section" id="an-aha-insight">
<h2>13.5. An “Aha!” insight<a class="headerlink" href="#an-aha-insight" title="Permalink to this headline">¶</a></h2>
<p>Often a high-level insight into the problem can make the programming much easier.</p>
<p>In this case, the insight is that a <tt class="docutils literal"><span class="pre">MyTime</span></tt> object is really a
three-digit number in base 60! The <tt class="docutils literal"><span class="pre">second</span></tt>
component is the ones column, the <tt class="docutils literal"><span class="pre">minute</span></tt> component is the sixties column,
and the <tt class="docutils literal"><span class="pre">hour</span></tt> component is the thirty-six hundreds column.</p>
<p>When we wrote <tt class="docutils literal"><span class="pre">add_time</span></tt> and <tt class="docutils literal"><span class="pre">increment</span></tt>, we were effectively doing
addition in base 60, which is why we had to carry from one column to the next.</p>
<p>This observation suggests another approach to the whole problem — we can
convert a <tt class="docutils literal"><span class="pre">MyTime</span></tt> object into a single number and take advantage of the fact
that the computer knows how to do arithmetic with numbers. The following
method is added to the <tt class="docutils literal"><span class="pre">MyTime</span></tt> class to convert any instance into
a corresponding number of seconds:</p>
<div id="mytimetosec" class="pywindow" >
<div id="mytimetosec_code_div" style="display: block">
<textarea rows="26" id="mytimetosec_code" class="active_code" prefixcode="undefined">
class MyTime:
def __init__(self, hrs=0, mins=0, secs=0):
""" Create a MyTime object initialized to hrs, mins, secs """
self.hours = hrs
self.minutes = mins
self.seconds = secs
def __str__(self):
timeString = ""
if self.hours < 10:
timeString += "0"
timeString += str(self.hours) + ":"
if self.minutes < 10:
timeString += "0"
timeString += str(self.minutes) + ":"
if self.seconds < 10:
timeString += "0"
timeString += str(self.seconds)
return timeString
def to_seconds(self):
""" Return the number of seconds represented
by this instance
"""
return self.hours * 3600 + self.minutes * 60 + self.seconds</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['mytimetosec_code'] = true;
pythonTool.readOnlyFlags['mytimetosec_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="mytimetosec_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="mytimetosec_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="mytimetosec_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='mytimetosec_error'></div>
<div style="text-align: center">
<canvas id="mytimetosec_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="mytimetosec_suffix" style="display:none">
</pre>
<pre id="mytimetosec_pre" class="active_out">
</pre>
<div id="mytimetosec_files" class="ac-files ac-files-hidden"></div>
</div>
<p>Now, all we need is a way to convert from an integer back to a <tt class="docutils literal"><span class="pre">MyTime</span></tt> object.
Supposing we have <tt class="docutils literal"><span class="pre">tsecs</span></tt> seconds, some integer division and mod operators
can do this for us:</p>
<div id="timeconversion" class="pywindow" >
<div id="timeconversion_code_div" style="display: block">
<textarea rows="4" id="timeconversion_code" class="active_code" prefixcode="undefined">
hrs = tsecs // 3600
leftoversecs = tsecs % 3600
mins = leftoversecs // 60
secs = leftoversecs % 60</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['timeconversion_code'] = false;
pythonTool.readOnlyFlags['timeconversion_code'] = true;
</script>
<div id='timeconversion_error'></div>
<pre id="timeconversion_suffix" style="display:none">
</pre>
</div>
<p>You might have to think a bit to convince yourself that this technique to
convert from one base to another is correct.</p>
<p>In OOP we’re really trying to wrap together the data and the operations
that apply to it. So we’d like to have this logic inside the <tt class="docutils literal"><span class="pre">MyTime</span></tt>
class. A good solution is to rewrite the class initializer so that it can
cope with initial values of seconds or minutes that are outside the
<strong>normalized</strong> values. (A normalized time would be something
like 3 hours 12 minutes and 20 seconds. The same time, but unnormalized
could be 2 hours 70 minutes and 140 seconds.)</p>
<p>Let’s rewrite a more powerful initializer for <tt class="docutils literal"><span class="pre">MyTime</span></tt>:</p>
<div id="mytimebetterinit" class="pywindow" >
<div id="mytimebetterinit_code_div" style="display: block">
<textarea rows="32" id="mytimebetterinit_code" class="active_code" prefixcode="undefined">
class MyTime:
def __init__(self, hrs=0, mins=0, secs=0):
""" Create a new MyTime object initialized to hrs, mins, secs.
The values of mins and secs may be outside the range 0-59,
but the resulting MyTime object will be normalized.
"""
# Calculate total seconds to represent
totalsecs = hrs*3600 + mins*60 + secs
self.hours = totalsecs // 3600 # Split in h, m, s
leftoversecs = totalsecs % 3600
self.minutes = leftoversecs // 60
self.seconds = leftoversecs % 60
def __str__(self):
timeString = ""
if self.hours < 10:
timeString += "0"
timeString += str(self.hours) + ":"
if self.minutes < 10:
timeString += "0"
timeString += str(self.minutes) + ":"
if self.seconds < 10:
timeString += "0"
timeString += str(self.seconds)
return timeString
def to_seconds(self):
""" Return the number of seconds represented
by this instance
"""
return self.hours * 3600 + self.minutes * 60 + self.seconds</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['mytimebetterinit_code'] = true;
pythonTool.readOnlyFlags['mytimebetterinit_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="mytimebetterinit_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="mytimebetterinit_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="mytimebetterinit_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='mytimebetterinit_error'></div>
<div style="text-align: center">
<canvas id="mytimebetterinit_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="mytimebetterinit_suffix" style="display:none">
</pre>
<pre id="mytimebetterinit_pre" class="active_out">
</pre>
<div id="mytimebetterinit_files" class="ac-files ac-files-hidden"></div>
</div>
<p>Now we can rewrite <tt class="docutils literal"><span class="pre">add_time</span></tt> like this:</p>
<div id="addtimebest" class="pywindow" >
<div id="addtimebest_code_div" style="display: block">
<textarea rows="41" id="addtimebest_code" class="active_code" prefixcode="undefined">
class MyTime:
def __init__(self, hrs=0, mins=0, secs=0):
""" Create a new MyTime object initialized to hrs, mins, secs.
The values of mins and secs may be outside the range 0-59,
but the resulting MyTime object will be normalized.
"""
# Calculate total seconds to represent
totalsecs = hrs*3600 + mins*60 + secs
self.hours = totalsecs // 3600 # Split in h, m, s
leftoversecs = totalsecs % 3600
self.minutes = leftoversecs // 60
self.seconds = leftoversecs % 60
def __str__(self):
timeString = ""
if self.hours < 10:
timeString += "0"
timeString += str(self.hours) + ":"
if self.minutes < 10:
timeString += "0"
timeString += str(self.minutes) + ":"
if self.seconds < 10:
timeString += "0"
timeString += str(self.seconds)
return timeString
def to_seconds(self):
""" Return the number of seconds represented
by this instance
"""
return self.hours * 3600 + self.minutes * 60 + self.seconds
def add_time(t1, t2):
secs = t1.to_seconds() + t2.to_seconds()
return MyTime(0, 0, secs)
currentTime = MyTime(6, 32, 40)
breadTime = MyTime(3, 35, 50)
doneTime = add_time(currentTime, breadTime)
print(doneTime)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['addtimebest_code'] = true;
pythonTool.readOnlyFlags['addtimebest_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="addtimebest_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="addtimebest_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="addtimebest_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='addtimebest_error'></div>
<div style="text-align: center">
<canvas id="addtimebest_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="addtimebest_suffix" style="display:none">
</pre>
<pre id="addtimebest_pre" class="active_out">
</pre>
<div id="addtimebest_files" class="ac-files ac-files-hidden"></div>
</div>
<p>This version is much shorter than the original, and it is much easier to
demonstrate or reason that it is correct.</p>
</div>
<div class="section" id="generalization">
<span id="index-2"></span><h2>13.6. Generalization<a class="headerlink" href="#generalization" title="Permalink to this headline">¶</a></h2>
<p>In some ways, converting from base 60 to base 10 and back is harder than just
dealing with times. Base conversion is more abstract; our intuition for dealing
with times is better.</p>
<p>But if we have the insight to treat times as base 60 numbers and make the
investment of writing the conversions, we get a program that is shorter,
easier to read and debug, and more reliable.</p>
<p>It is also easier to add features later. For example, imagine subtracting two
<tt class="docutils literal"><span class="pre">MyTime</span></tt> objects to find the duration between them. The naive approach would be to
implement subtraction with borrowing. Using the conversion functions would be
easier and more likely to be correct.</p>
<p>Ironically, sometimes making a problem harder (or more general) makes the
programming easier, because there are fewer special cases and fewer opportunities
for error.</p>
<div class="admonition-specialization-versus-generalization admonition">
<p class="first admonition-title">Specialization versus Generalization</p>
<p>Computer Scientists are generally fond of specializing their types, while mathematicians
often take the opposite approach, and generalize everything.</p>
<p>What do we mean by this?</p>
<p>If we ask a mathematician to solve a problem involving weekdays, days of the century,
playing cards, time, or dominoes, their most likely response is
to observe that all these objects can be represented by integers. Playing cards, for example,
can be numbered from 0 to 51. Days within the century can be numbered. Mathematicians will say
<em>“These things are enumerable — the elements can be uniquely numbered (and we can
reverse this numbering to get back to the original concept). So let’s number
them, and confine our thinking to integers. Luckily, we have powerful techniques and a
good understanding of integers, and so our abstractions — the way we tackle and simplify
these problems — is to try to reduce them to problems about integers.”</em></p>
<p>Computer Scientists tend to do the opposite. We will argue that there are many integer
operations that are simply not meaningful for dominoes, or for days of the century. So
we’ll often define new specialized types, like <tt class="docutils literal"><span class="pre">MyTime</span></tt>, because we can restrict,
control, and specialize the operations that are possible. Object-oriented programming
is particularly popular because it gives us a good way to bundle methods and specialized data
into a new type.</p>
<p class="last">Both approaches are powerful problem-solving techniques. Often it may help to try to
think about the problem from both points of view — <em>“What would happen if I tried to reduce
everything to very few primitive types?”</em>, versus
<em>“What would happen if this thing had its own specialized type?”</em></p>
</div>
</div>
<div class="section" id="operator-overloading">
<h2>13.7. Operator overloading<a class="headerlink" href="#operator-overloading" title="Permalink to this headline">¶</a></h2>
<p>Some languages, including Python, make it possible to have different meanings for
the same operator when applied to different types. For example, <tt class="docutils literal"><span class="pre">+</span></tt> in Python
means quite different things for integers and for strings. This feature is called
<strong>operator overloading</strong>.</p>
<p>It is especially useful when programmers can also overload the operators for their
own user-defined types.</p>
<p>For example, to override the addition operator <tt class="docutils literal"><span class="pre">+</span></tt>, we can provide a method named
<tt class="docutils literal"><span class="pre">__add__</span></tt>:</p>
<div id="addtimeoverload" class="pywindow" >
<div id="addtimeoverload_code_div" style="display: block">
<textarea rows="41" id="addtimeoverload_code" class="active_code" prefixcode="undefined">
class MyTime:
def __init__(self, hrs=0, mins=0, secs=0):
""" Create a new MyTime object initialized to hrs, mins, secs.
The values of mins and secs may be outside the range 0-59,
but the resulting MyTime object will be normalized.
"""
# Calculate total seconds to represent
totalsecs = hrs*3600 + mins*60 + secs
self.hours = totalsecs // 3600 # Split in h, m, s
leftoversecs = totalsecs % 3600
self.minutes = leftoversecs // 60
self.seconds = leftoversecs % 60
def __str__(self):
timeString = ""
if self.hours < 10:
timeString += "0"
timeString += str(self.hours) + ":"
if self.minutes < 10:
timeString += "0"
timeString += str(self.minutes) + ":"
if self.seconds < 10:
timeString += "0"
timeString += str(self.seconds)
return timeString
def to_seconds(self):
""" Return the number of seconds represented
by this instance
"""
return self.hours * 3600 + self.minutes * 60 + self.seconds
def __add__(self, other):
secs = self.to_seconds() + other.to_seconds()
return MyTime(0, 0, secs)
currentTime = MyTime(6, 32, 40)
breadTime = MyTime(3, 35, 50)
doneTime = currentTime + breadTime
print(doneTime)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['addtimeoverload_code'] = true;
pythonTool.readOnlyFlags['addtimeoverload_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="addtimeoverload_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="addtimeoverload_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="addtimeoverload_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='addtimeoverload_error'></div>
<div style="text-align: center">
<canvas id="addtimeoverload_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="addtimeoverload_suffix" style="display:none">
</pre>
<pre id="addtimeoverload_pre" class="active_out">
</pre>
<div id="addtimeoverload_files" class="ac-files ac-files-hidden"></div>
</div>
<p>As usual, the first parameter is the object on which the method is invoked. The
second parameter is conveniently named <tt class="docutils literal"><span class="pre">other</span></tt> to distinguish it from
<tt class="docutils literal"><span class="pre">self</span></tt>. To add two <tt class="docutils literal"><span class="pre">MyTime</span></tt> objects, we create and return a new <tt class="docutils literal"><span class="pre">MyTime</span></tt> object
that contains their sum.</p>
<p>Now, when we apply the <tt class="docutils literal"><span class="pre">+</span></tt> operator to <tt class="docutils literal"><span class="pre">MyTime</span></tt> objects, Python invokes
the <tt class="docutils literal"><span class="pre">__add__</span></tt> method that we have written. The expression <tt class="docutils literal"><span class="pre">currentTime</span> <span class="pre">+</span> <span class="pre">breadTime</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">currentTime.__add__(breadTime)</span></tt>, but obviously
more elegant.</p>
<p>As an exercise, add a method <tt class="docutils literal"><span class="pre">__sub__(self,</span> <span class="pre">other)</span></tt> that
overloads the subtraction operator, and try it out.</p>
</div>
<div class="section" id="glossary">
<h2>13.8. Glossary<a class="headerlink" href="#glossary" title="Permalink to this headline">¶</a></h2>
<dl class="glossary docutils">
<dt id="term-functional-programming-style">functional programming style</dt>
<dd>A style of program design in which the majority of functions are pure.</dd>
<dt id="term-modifier">modifier</dt>
<dd>A function or method that changes one or more of the objects it receives as
parameters. Most modifier functions are void (do not return a value).</dd>
<dt id="term-normalized">normalized</dt>
<dd>Data is said to be normalized if it fits into some reduced range or set of rules.
We usually normalize our angles to values in the range [0..360). We normalize