-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclasses.html
executable file
·1116 lines (913 loc) · 48.2 KB
/
classes.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>12. Classes and Objects — the Basics — 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="13. More Classes and Objects" href="oop.html" />
<link rel="prev" title="11. Recursion" href="recursion.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="oop.html" title="13. More Classes and Objects"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="recursion.html" title="11. Recursion"
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="classes-and-objects-the-basics">
<h1>12. Classes and Objects — the Basics<a class="headerlink" href="#classes-and-objects-the-basics" title="Permalink to this headline">¶</a></h1>
<div class="section" id="object-oriented-programming">
<span id="index-0"></span><h2>12.1. Object-oriented programming<a class="headerlink" href="#object-oriented-programming" title="Permalink to this headline">¶</a></h2>
<p>Python is an <strong>object-oriented programming language</strong>, which means that it
provides features that support <a class="reference external" href="http://en.wikipedia.org/wiki/Object-oriented_programming">object-oriented programming</a> (<strong>OOP</strong>).</p>
<p>Object-oriented programming has its roots in the 1960s, but it wasn’t until the
mid 1980s that it became the main <a class="reference external" href="http://en.wikipedia.org/wiki/Programming_paradigm">programming paradigm</a> used in the creation
of new software. It was developed as a way to handle the rapidly increasing
size and complexity of software systems, and to make it easier to modify these
large and complex systems over time.</p>
<p>Up to now, most of the programs we have been writing use a <a class="reference external" href="http://en.wikipedia.org/wiki/Procedural_programming">procedural programming</a> paradigm. In
procedural programming the focus is on writing functions or <em>procedures</em> which
operate on data. In object-oriented programming the focus is on the creation of
<strong>objects</strong> which contain both data and functionality together. (We have seen turtle
objects, string objects, and random number generators, to name a few places where
we’ve already worked with objects.)</p>
<p>Usually, each object definition corresponds to some object or concept in the real
world, and the functions that operate on that object correspond to the ways
real-world objects interact.</p>
</div>
<div class="section" id="user-defined-compound-data-types">
<span id="index-1"></span><h2>12.2. User-defined compound data types<a class="headerlink" href="#user-defined-compound-data-types" title="Permalink to this headline">¶</a></h2>
<p>We’ve already seen classes like <tt class="docutils literal"><span class="pre">str</span></tt>, <tt class="docutils literal"><span class="pre">int</span></tt>, <tt class="docutils literal"><span class="pre">float</span></tt> and <tt class="docutils literal"><span class="pre">Turtle</span></tt>.
We are now ready to create our own user-defined class: the <tt class="docutils literal"><span class="pre">Point</span></tt>.</p>
<p>Consider the concept of a mathematical point. In two dimensions, a point is two
numbers (coordinates) that are treated collectively as a single object.
Points are often written in parentheses with a comma
separating the coordinates. For example, <tt class="docutils literal"><span class="pre">(0,</span> <span class="pre">0)</span></tt> represents the origin, and
<tt class="docutils literal"><span class="pre">(x,</span> <span class="pre">y)</span></tt> represents the point <tt class="docutils literal"><span class="pre">x</span></tt> units to the right and <tt class="docutils literal"><span class="pre">y</span></tt> units up
from the origin.</p>
<p>Some of the typical operations that one associates with points might be
calculating the distance of a point from the origin, or from another point,
or finding a midpoint of two points, or asking if a point falls within a
given rectangle or circle. We’ll shortly see how we can organize these
together with the data.</p>
<p>A natural way to represent a point in Python is with two numeric values. The
question, then, is how to group these two values into a compound object. The
quick and dirty solution is to use a tuple, and for some applications
that might be a good choice.</p>
<p>An alternative is to define a new <strong>class</strong>. This approach involves a
bit more effort, but it has advantages that will be apparent soon.
We’ll want our points to each have an <tt class="docutils literal"><span class="pre">x</span></tt> and a <tt class="docutils literal"><span class="pre">y</span></tt> attribute,
so our first class definition looks like this:</p>
<div id="pointdef" class="pywindow" >
<div id="pointdef_code_div" style="display: block">
<textarea rows="7" id="pointdef_code" class="active_code" prefixcode="undefined">
class Point:
""" Point class represents and manipulates x,y coords. """
def __init__(self):
""" Create a new point at the origin """
self.x = 0
self.y = 0</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['pointdef_code'] = true;
pythonTool.readOnlyFlags['pointdef_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="pointdef_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="pointdef_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="pointdef_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='pointdef_error'></div>
<div style="text-align: center">
<canvas id="pointdef_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="pointdef_suffix" style="display:none">
</pre>
<pre id="pointdef_pre" class="active_out">
</pre>
<div id="pointdef_files" class="ac-files ac-files-hidden"></div>
</div>
<p>Class definitions can appear anywhere in a program, but they are usually near
the beginning (after the <tt class="docutils literal"><span class="pre">import</span></tt> statements). Some programmers and languages
prefer to put every class in a module of its own — we won’t do that here.
The syntax rules for a class
definition are the same as for other compound statements. There is a header
which begins with the keyword, <tt class="docutils literal"><span class="pre">class</span></tt>, followed by the name of the class,
and ending with a colon. Indentation levels tell us where the class ends.</p>
<p>If the first line after the class header is a string, it becomes
the docstring of the class, and will be recognized by various tools. (This
is also the way docstrings work in functions.)</p>
<p>Every class should have a method with the special name <tt class="docutils literal"><span class="pre">__init__</span></tt>.
This <strong>initializer method</strong> is automatically called whenever a new
instance of <tt class="docutils literal"><span class="pre">Point</span></tt> is created. It gives the programmer the opportunity
to set up the attributes required within the new instance by giving them
their initial state/values. The <tt class="docutils literal"><span class="pre">self</span></tt> parameter (we could choose any
other name, but <tt class="docutils literal"><span class="pre">self</span></tt> is the convention) is automatically set to reference
the newly created object that needs to be initialized.</p>
<p>So let’s use our new <tt class="docutils literal"><span class="pre">Point</span></tt> class now:</p>
<div id="pointcreate" class="pywindow" >
<div id="pointcreate_code_div" style="display: block">
<textarea rows="12" id="pointcreate_code" class="active_code" prefixcode="undefined">
class Point:
""" Point class represents and manipulates x,y coords. """
def __init__(self):
""" Create a new point at the origin """
self.x = 0
self.y = 0
p = Point() # Instantiate an object of type Point
q = Point() # Make a second point
print(p.x, p.y, q.x, q.y) # Each point object has its own x and y</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['pointcreate_code'] = true;
pythonTool.readOnlyFlags['pointcreate_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="pointcreate_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="pointcreate_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="pointcreate_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='pointcreate_error'></div>
<div style="text-align: center">
<canvas id="pointcreate_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="pointcreate_suffix" style="display:none">
</pre>
<pre id="pointcreate_pre" class="active_out">
</pre>
<div id="pointcreate_files" class="ac-files ac-files-hidden"></div>
</div>
<p>This program prints:</p>
<blockquote>
<div><div class="highlight-python3"><div class="highlight"><pre><span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span>
</pre></div>
</div>
</div></blockquote>
<p>because during the initialization of the objects, we created two
attributes called <tt class="docutils literal"><span class="pre">x</span></tt> and <tt class="docutils literal"><span class="pre">y</span></tt> for each, and gave them both the value 0.</p>
<p>This should look familiar — we’ve used classes before to create
more than one object:</p>
<div id="turtleanalogy" class="pywindow" >
<div id="turtleanalogy_code_div" style="display: block">
<textarea rows="4" id="turtleanalogy_code" class="active_code" prefixcode="undefined">
from turtle import Turtle
tess = Turtle() # Instantiate objects of type Turtle
alex = Turtle()</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['turtleanalogy_code'] = false;
pythonTool.readOnlyFlags['turtleanalogy_code'] = true;
</script>
<div id='turtleanalogy_error'></div>
<pre id="turtleanalogy_suffix" style="display:none">
</pre>
</div>
<p>The variables <tt class="docutils literal"><span class="pre">p</span></tt> and <tt class="docutils literal"><span class="pre">q</span></tt> are assigned references to two new <tt class="docutils literal"><span class="pre">Point</span></tt> objects.
A function like <tt class="docutils literal"><span class="pre">Turtle</span></tt> or <tt class="docutils literal"><span class="pre">Point</span></tt> that creates a new object instance
is called a <strong>constructor</strong>, and every class automatically provides a
constructor function which is named the same as the class.</p>
<p>It may be helpful to think of a class as a <em>factory</em> for making objects.
The class itself isn’t an instance of a point, but it contains the machinery
to make point instances. Every time we call the constructor, we’re asking
the factory to make us a new object. As the object comes off the
production line, its initialization method is executed to
get the object properly set up with its factory default settings.</p>
<p>The combined process of “make me a new object” and “get its settings initialized
to the factory default settings” is called <strong>instantiation</strong>.</p>
</div>
<div class="section" id="attributes">
<span id="index-2"></span><h2>12.3. Attributes<a class="headerlink" href="#attributes" title="Permalink to this headline">¶</a></h2>
<p>Like real world objects, object instances have both attributes and methods.</p>
<p>We can modify the attributes in an instance using dot notation:</p>
<div id="pointmodify" class="pywindow" >
<div id="pointmodify_code_div" style="display: block">
<textarea rows="16" id="pointmodify_code" class="active_code" prefixcode="undefined">
class Point:
""" Point class represents and manipulates x,y coords. """
def __init__(self):
""" Create a new point at the origin """
self.x = 0
self.y = 0
p = Point() # Instantiate an object of type Point
q = Point() # Make a second point
# change attributes of p
p.x = 3
p.y = 4
print(p.x, p.y, q.x, q.y) # Each point object has its own x and y</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['pointmodify_code'] = true;
pythonTool.readOnlyFlags['pointmodify_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="pointmodify_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="pointmodify_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="pointmodify_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='pointmodify_error'></div>
<div style="text-align: center">
<canvas id="pointmodify_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="pointmodify_suffix" style="display:none">
</pre>
<pre id="pointmodify_pre" class="active_out">
</pre>
<div id="pointmodify_files" class="ac-files ac-files-hidden"></div>
</div>
<p>Both modules and instances create
their own namespaces, and the syntax for accessing names contained in each,
called <strong>attributes</strong>, is the same. In this case the attribute we are selecting
is a data item from an instance.</p>
<p>The following state diagram shows the result of these assignments:</p>
<blockquote>
<div><img alt="Point state diagram" src="_images/point.png" />
</div></blockquote>
<p>The variable <tt class="docutils literal"><span class="pre">p</span></tt> refers to a <tt class="docutils literal"><span class="pre">Point</span></tt> object, which contains two attributes.
Each attribute refers to a number.</p>
<p>We can access the value of an attribute using the same syntax:</p>
<div id="pointattr" class="pywindow" >
<div id="pointattr_code_div" style="display: block">
<textarea rows="17" id="pointattr_code" class="active_code" prefixcode="undefined">
class Point:
""" Point class represents and manipulates x,y coords. """
def __init__(self):
""" Create a new point at the origin """
self.x = 0
self.y = 0
p = Point() # Instantiate an object of type Point
q = Point() # Make a second point
# change attributes of p
p.x = 3
p.y = 4
x = p.x
print(x)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['pointattr_code'] = true;
pythonTool.readOnlyFlags['pointattr_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="pointattr_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="pointattr_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="pointattr_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='pointattr_error'></div>
<div style="text-align: center">
<canvas id="pointattr_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="pointattr_suffix" style="display:none">
</pre>
<pre id="pointattr_pre" class="active_out">
</pre>
<div id="pointattr_files" class="ac-files ac-files-hidden"></div>
</div>
<p>The expression <tt class="docutils literal"><span class="pre">p.x</span></tt> means, “Go to the object <tt class="docutils literal"><span class="pre">p</span></tt> refers to and get the
value of <tt class="docutils literal"><span class="pre">x</span></tt>”. In this case, we assign that value to a variable named <tt class="docutils literal"><span class="pre">x</span></tt>.
There is no conflict between the variable <tt class="docutils literal"><span class="pre">x</span></tt> (in the global namespace here)
and the attribute <tt class="docutils literal"><span class="pre">x</span></tt> (in the namespace belonging to the instance). The
purpose of dot notation is to fully qualify which variable we are referring to
unambiguously.</p>
<p>We can use dot notation as part of any expression, so the following statements
are legal:</p>
<div id="pointuseattr" class="pywindow" >
<div id="pointuseattr_code_div" style="display: block">
<textarea rows="18" id="pointuseattr_code" class="active_code" prefixcode="undefined">
class Point:
""" Point class represents and manipulates x,y coords. """
def __init__(self):
""" Create a new point at the origin """
self.x = 0
self.y = 0
p = Point() # Instantiate an object of type Point
q = Point() # Make a second point
# change attributes of p
p.x = 3
p.y = 4
print("(x="+str(p.x)+", y="+str(p.y)+")")
distanceSquaredFromOrigin = p.x * p.x + p.y * p.y
print(distanceSquaredFromOrigin)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['pointuseattr_code'] = true;
pythonTool.readOnlyFlags['pointuseattr_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="pointuseattr_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="pointuseattr_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="pointuseattr_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='pointuseattr_error'></div>
<div style="text-align: center">
<canvas id="pointuseattr_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="pointuseattr_suffix" style="display:none">
</pre>
<pre id="pointuseattr_pre" class="active_out">
</pre>
<div id="pointuseattr_files" class="ac-files ac-files-hidden"></div>
</div>
</div>
<div class="section" id="improving-our-initializer">
<h2>12.4. Improving our initializer<a class="headerlink" href="#improving-our-initializer" title="Permalink to this headline">¶</a></h2>
<p>To create a point at position (7, 6) currently needs three lines of code:</p>
<div id="pointinitbad" class="pywindow" >
<div id="pointinitbad_code_div" style="display: block">
<textarea rows="3" id="pointinitbad_code" class="active_code" prefixcode="undefined">
p = Point()
p.x = 7
p.y = 6</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['pointinitbad_code'] = false;
pythonTool.readOnlyFlags['pointinitbad_code'] = true;
</script>
<div id='pointinitbad_error'></div>
<pre id="pointinitbad_suffix" style="display:none">
</pre>
</div>
<p>We can make our class constructor more general by placing extra parameters into
the <tt class="docutils literal"><span class="pre">__init__</span></tt> method, as shown in this example:</p>
<div id="pointinitimproved" class="pywindow" >
<div id="pointinitimproved_code_div" style="display: block">
<textarea rows="9" id="pointinitimproved_code" class="active_code" prefixcode="undefined">
class Point:
""" Point class represents and manipulates x,y coords. """
def __init__(self, x=0, y=0):
""" Create a new point at the origin """
self.x = x
self.y = y
# Other statements outside the class continue below here.</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['pointinitimproved_code'] = false;
pythonTool.readOnlyFlags['pointinitimproved_code'] = true;
</script>
<div id='pointinitimproved_error'></div>
<pre id="pointinitimproved_suffix" style="display:none">
</pre>
</div>
<p>The <tt class="docutils literal"><span class="pre">x</span></tt> and <tt class="docutils literal"><span class="pre">y</span></tt> parameters here are both optional. If the caller does not
supply arguments, they’ll get the default values of 0. Here is our improved class
in action:</p>
<div id="pointinitexample" class="pywindow" >
<div id="pointinitexample_code_div" style="display: block">
<textarea rows="12" id="pointinitexample_code" class="active_code" prefixcode="undefined">
class Point:
""" Point class represents and manipulates x,y coords. """
def __init__(self, x=0, y=0):
""" Create a new point at the origin """
self.x = x
self.y = y
p = Point(4, 2)
q = Point(6, 3)
r = Point() # r represents the origin (0, 0)
print(p.x, q.y, r.x) # should print 4 3 0</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['pointinitexample_code'] = true;
pythonTool.readOnlyFlags['pointinitexample_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="pointinitexample_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="pointinitexample_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="pointinitexample_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='pointinitexample_error'></div>
<div style="text-align: center">
<canvas id="pointinitexample_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="pointinitexample_suffix" style="display:none">
</pre>
<pre id="pointinitexample_pre" class="active_out">
</pre>
<div id="pointinitexample_files" class="ac-files ac-files-hidden"></div>
</div>
</div>
<div class="section" id="adding-other-methods-to-our-class">
<h2>12.5. Adding other methods to our class<a class="headerlink" href="#adding-other-methods-to-our-class" title="Permalink to this headline">¶</a></h2>
<p>The key advantage of using a class like <tt class="docutils literal"><span class="pre">Point</span></tt> rather than a simple
tuple <tt class="docutils literal"><span class="pre">(6,</span> <span class="pre">7)</span></tt> now becomes apparent. We can add methods to
the <tt class="docutils literal"><span class="pre">Point</span></tt> class that are sensible operations for points, but
which may not be appropriate for other tuples like <tt class="docutils literal"><span class="pre">(25,</span> <span class="pre">12)</span></tt> which might
represent, say, a day and a month, e.g. Christmas day. So being able
to calculate the distance from the origin is sensible for
points, but not for (day, month) data. For (day, month) data,
we’d like different operations, perhaps to find what day of the week
it will fall on in 2020.</p>
<p>Creating a class like <tt class="docutils literal"><span class="pre">Point</span></tt> brings an exceptional
amount of “organizational power” to our programs, and to our thinking.
We can group together the sensible operations, and the kinds of data
they apply to, and each instance of the class can have its own state.</p>
<p>A <strong>method</strong> behaves like a function but it is invoked on a specific
instance, e.g. <tt class="docutils literal"><span class="pre">tess.right(90)</span></tt>. Like a data
attribute, methods are accessed using dot notation.</p>
<p>Let’s add another method, <tt class="docutils literal"><span class="pre">distance_from_origin</span></tt>, to see better how methods
work:</p>
<div id="pointmethod" class="pywindow" >
<div id="pointmethod_code_div" style="display: block">
<textarea rows="19" id="pointmethod_code" class="active_code" prefixcode="undefined">
class Point:
""" Point class represents and manipulates x,y coords. """
def __init__(self, x=0, y=0):
""" Create a new point at the origin """
self.x = x
self.y = y
def distance_from_origin(self):
""" Compute my distance from the origin """
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
p = Point(3, 4)
q = Point(5, 12)
r = Point() # r represents the origin (0, 0)
print(p.distance_from_origin()) # should be 5
print(q.distance_from_origin()) # should be 13
print(r.distance_from_origin()) # should be 0</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['pointmethod_code'] = true;
pythonTool.readOnlyFlags['pointmethod_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="pointmethod_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="pointmethod_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="pointmethod_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='pointmethod_error'></div>
<div style="text-align: center">
<canvas id="pointmethod_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="pointmethod_suffix" style="display:none">
</pre>
<pre id="pointmethod_pre" class="active_out">
</pre>
<div id="pointmethod_files" class="ac-files ac-files-hidden"></div>
</div>
<p>When defining a method, the first parameter refers to the instance being
manipulated. As already noted, it is customary to name this parameter <tt class="docutils literal"><span class="pre">self</span></tt>.</p>
<p>Notice that the caller of <tt class="docutils literal"><span class="pre">distance_from_origin</span></tt> does not explicitly
supply an argument to match the <tt class="docutils literal"><span class="pre">self</span></tt> parameter — this is done for
us, behind our back.</p>
</div>
<div class="section" id="instances-as-arguments-and-parameters">
<h2>12.6. Instances as arguments and parameters<a class="headerlink" href="#instances-as-arguments-and-parameters" title="Permalink to this headline">¶</a></h2>
<p>We can pass an object as an argument in the usual way. We’ve already seen
this in some of the turtle examples, where we passed the turtle to
some function like <tt class="docutils literal"><span class="pre">draw_bar</span></tt> in the chapter titled <cite>Conditionals</cite>,
so that the function could control and use whatever turtle instance we passed to it.</p>
<p>Be aware that our variable only holds a reference to an object, so passing <tt class="docutils literal"><span class="pre">tess</span></tt>
into a function creates an alias: both the caller and the called function
now have a reference, but there is only one turtle!</p>
<p>Here is a simple function involving our new <tt class="docutils literal"><span class="pre">Point</span></tt> objects:</p>
<div id="printpointfunc" class="pywindow" >
<div id="printpointfunc_code_div" style="display: block">
<textarea rows="17" id="printpointfunc_code" class="active_code" prefixcode="undefined">
class Point:
""" Point class represents and manipulates x,y coords. """
def __init__(self, x=0, y=0):
""" Create a new point at the origin """
self.x = x
self.y = y
def distance_from_origin(self):
""" Compute my distance from the origin """
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
def print_point(pt):
print("("+str(pt.x)+","+str(pt.y)+")")
p = Point(3, 4)
print_point(p)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['printpointfunc_code'] = true;
pythonTool.readOnlyFlags['printpointfunc_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="printpointfunc_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="printpointfunc_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="printpointfunc_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='printpointfunc_error'></div>
<div style="text-align: center">
<canvas id="printpointfunc_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="printpointfunc_suffix" style="display:none">
</pre>
<pre id="printpointfunc_pre" class="active_out">
</pre>
<div id="printpointfunc_files" class="ac-files ac-files-hidden"></div>
</div>
</div>
<div class="section" id="converting-an-instance-to-a-string">
<h2>12.7. Converting an instance to a string<a class="headerlink" href="#converting-an-instance-to-a-string" title="Permalink to this headline">¶</a></h2>
<p>Most object-oriented programmers probably would not do what we’ve just done in <tt class="docutils literal"><span class="pre">print_point</span></tt>.
When we’re working with classes and objects, a preferred alternative
is to add a new method to the class. And we don’t like chatterbox methods that call
<tt class="docutils literal"><span class="pre">print</span></tt>. A better approach is to have a method so that every instance
can produce a string representation of itself. Let’s initially
call it <tt class="docutils literal"><span class="pre">to_string</span></tt>:</p>
<div id="printpointmethod" class="pywindow" >
<div id="printpointmethod_code_div" style="display: block">
<textarea rows="17" id="printpointmethod_code" class="active_code" prefixcode="undefined">
class Point:
""" Point class represents and manipulates x,y coords. """
def __init__(self, x=0, y=0):
""" Create a new point at the origin """
self.x = x
self.y = y
def distance_from_origin(self):
""" Compute my distance from the origin """
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
def to_string(self):
return "("+str(self.x)+","+str(self.y)+")"
p = Point(3, 4)
print(p.to_string())</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['printpointmethod_code'] = true;
pythonTool.readOnlyFlags['printpointmethod_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="printpointmethod_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="printpointmethod_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="printpointmethod_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='printpointmethod_error'></div>
<div style="text-align: center">
<canvas id="printpointmethod_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="printpointmethod_suffix" style="display:none">
</pre>
<pre id="printpointmethod_pre" class="active_out">
</pre>
<div id="printpointmethod_files" class="ac-files ac-files-hidden"></div>
</div>
<p>But don’t we already have a <tt class="docutils literal"><span class="pre">str</span></tt> type converter that can
turn our object into a string? Yes! And doesn’t <tt class="docutils literal"><span class="pre">print</span></tt>
automatically use this when printing things? Yes again!
But these automatic mechanisms do not yet do exactly what we want:</p>
<div id="printobjdirectly" class="pywindow" >
<div id="printobjdirectly_code_div" style="display: block">
<textarea rows="18" id="printobjdirectly_code" class="active_code" prefixcode="undefined">
class Point:
""" Point class represents and manipulates x,y coords. """
def __init__(self, x=0, y=0):
""" Create a new point at the origin """
self.x = x
self.y = y
def distance_from_origin(self):
""" Compute my distance from the origin """
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
def to_string(self):
return "("+str(self.x)+","+str(self.y)+")"
p = Point(3, 4)
print(str(p))
print(p)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['printobjdirectly_code'] = true;
pythonTool.readOnlyFlags['printobjdirectly_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="printobjdirectly_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="printobjdirectly_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="printobjdirectly_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='printobjdirectly_error'></div>
<div style="text-align: center">
<canvas id="printobjdirectly_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="printobjdirectly_suffix" style="display:none">
</pre>
<pre id="printobjdirectly_pre" class="active_out">
</pre>
<div id="printobjdirectly_files" class="ac-files ac-files-hidden"></div>
</div>
<p>Python has a clever trick up its sleeve to fix this. If we call our new
method <tt class="docutils literal"><span class="pre">__str__</span></tt> instead of <tt class="docutils literal"><span class="pre">to_string</span></tt>, the Python interpreter
will use our code whenever it needs to convert a <tt class="docutils literal"><span class="pre">Point</span></tt> to a string.
Let’s re-do this again, now:</p>
<div id="pointstrmethod" class="pywindow" >
<div id="pointstrmethod_code_div" style="display: block">
<textarea rows="18" id="pointstrmethod_code" class="active_code" prefixcode="undefined">
class Point:
""" Point class represents and manipulates x,y coords. """
def __init__(self, x=0, y=0):
""" Create a new point at the origin """
self.x = x
self.y = y
def distance_from_origin(self):
""" Compute my distance from the origin """
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
def __str__(self):
return "("+str(self.x)+","+str(self.y)+")"
p = Point(3, 4)
print(str(p))
print(p)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['pointstrmethod_code'] = true;
pythonTool.readOnlyFlags['pointstrmethod_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="pointstrmethod_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="pointstrmethod_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="pointstrmethod_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='pointstrmethod_error'></div>
<div style="text-align: center">
<canvas id="pointstrmethod_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="pointstrmethod_suffix" style="display:none">
</pre>
<pre id="pointstrmethod_pre" class="active_out">
</pre>
<div id="pointstrmethod_files" class="ac-files ac-files-hidden"></div>
</div>
</div>
<div class="section" id="instances-as-return-values">
<h2>12.8. Instances as return values<a class="headerlink" href="#instances-as-return-values" title="Permalink to this headline">¶</a></h2>
<p>Functions and methods can return instances. For example, given two <tt class="docutils literal"><span class="pre">Point</span></tt> objects,
find their midpoint. First we’ll write this as a regular function. The function creates and returns a new <tt class="docutils literal"><span class="pre">Point</span></tt> object:</p>
<div id="pointsmpfunc" class="pywindow" >
<div id="pointsmpfunc_code_div" style="display: block">
<textarea rows="25" id="pointsmpfunc_code" class="active_code" prefixcode="undefined">
class Point:
""" Point class represents and manipulates x,y coords. """
def __init__(self, x=0, y=0):
""" Create a new point at the origin """
self.x = x
self.y = y
def distance_from_origin(self):
""" Compute my distance from the origin """
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
def __str__(self):
return "("+str(self.x)+","+str(self.y)+")"
def midpoint(p1, p2):
""" Return the midpoint of points p1 and p2 """
mx = (p1.x + p2.x)/2
my = (p1.y + p2.y)/2
return Point(mx, my)
p = Point(3, 4)
q = Point(5, 12)
r = midpoint(p, q)
print(r)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['pointsmpfunc_code'] = true;
pythonTool.readOnlyFlags['pointsmpfunc_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="pointsmpfunc_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="pointsmpfunc_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="pointsmpfunc_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='pointsmpfunc_error'></div>
<div style="text-align: center">
<canvas id="pointsmpfunc_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="pointsmpfunc_suffix" style="display:none">
</pre>
<pre id="pointsmpfunc_pre" class="active_out">
</pre>
<div id="pointsmpfunc_files" class="ac-files ac-files-hidden"></div>
</div>
<p>Now let us do this as a method instead. Suppose we have a point object,
and wish to find the midpoint halfway between it and some other target point. This method is identical to the function, aside from some renaming.</p>
<div id="pointsmpmeth" class="pywindow" >
<div id="pointsmpmeth_code_div" style="display: block">
<textarea rows="25" id="pointsmpmeth_code" class="active_code" prefixcode="undefined">
class Point:
""" Point class represents and manipulates x,y coords. """
def __init__(self, x=0, y=0):
""" Create a new point at the origin """
self.x = x
self.y = y
def distance_from_origin(self):
""" Compute my distance from the origin """
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
def __str__(self):
return "("+str(self.x)+","+str(self.y)+")"
def halfway(self, target):
""" Return the halfway point between myself and the target """
mx = (self.x + target.x)/2
my = (self.y + target.y)/2
return Point(mx, my)
p = Point(3, 4)
q = Point(5, 12)
r = p.halfway(q)
print(r)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['pointsmpmeth_code'] = true;
pythonTool.readOnlyFlags['pointsmpmeth_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="pointsmpmeth_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="pointsmpmeth_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="pointsmpmeth_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='pointsmpmeth_error'></div>
<div style="text-align: center">
<canvas id="pointsmpmeth_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="pointsmpmeth_suffix" style="display:none">
</pre>
<pre id="pointsmpmeth_pre" class="active_out">
</pre>
<div id="pointsmpmeth_files" class="ac-files ac-files-hidden"></div>
</div>
<p>While this example assigns each point to a variable, this need not be done.
Just as function calls are composable, method calls and object instantiation
are also composable, leading to this alternative that uses no variables:</p>
<div id="pointsmpnovars" class="pywindow" >
<div id="pointsmpnovars_code_div" style="display: block">
<textarea rows="22" id="pointsmpnovars_code" class="active_code" prefixcode="undefined">
class Point:
""" Point class represents and manipulates x,y coords. """
def __init__(self, x=0, y=0):
""" Create a new point at the origin """
self.x = x
self.y = y
def distance_from_origin(self):
""" Compute my distance from the origin """
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
def __str__(self):
return "("+str(self.x)+","+str(self.y)+")"
def halfway(self, target):
""" Return the halfway point between myself and the target """
mx = (self.x + target.x)/2
my = (self.y + target.y)/2
return Point(mx, my)
print(Point(3, 4).halfway(Point(5, 12)))</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['pointsmpnovars_code'] = true;
pythonTool.readOnlyFlags['pointsmpnovars_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="pointsmpnovars_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="pointsmpnovars_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="pointsmpnovars_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='pointsmpnovars_error'></div>
<div style="text-align: center">
<canvas id="pointsmpnovars_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>