You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pretext/ClassesBasics/ImprovingourConstructor.ptx
+13-1Lines changed: 13 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,19 @@
5
5
provide some additional capability for the user to pass information to the constructor. Since constructors are simply specially named functions, we can use parameters (as we've seen before) to provide the specific information.</p>
6
6
<p>We can make our class constructor more general by putting extra parameters into
7
7
the <c>__init__</c> method, as shown in this codelens example.</p>
""" Point class for representing and manipulating x,y coordinates. """
12
+
13
+
def __init__(self, initX, initY):
14
+
""" Create a new point at the given coordinates. """
15
+
self.x = initX
16
+
self.y = initY
17
+
18
+
p = Point(7, 6)
19
+
</input>
20
+
</program>
9
21
<p>Now when we create new points, we supply the x and y coordinates as parameters. When the point is created, the values of <c>initX</c> and <c>initY</c> are assigned to the state of the object.</p>
10
22
<imagesource="ClassesBasics/Figures/objectpic5.png"width="50%"alt="Simple object has state and methods"/>
""" Point class for representing and manipulating x,y coordinates. """
84
+
85
+
def __init__(self):
86
+
""" Create a new point at the origin """
87
+
self.x = 0
88
+
self.y = 0
89
+
90
+
p = Point() # Instantiate an object of type Point
91
+
q = Point() # and make a second point
92
+
93
+
print("Nothing seems to have happened with the points")
94
+
</input>
95
+
</program>
81
96
<p>The following program adds a few print statements. You can see that the output suggests that each one is a <c>Point object</c>.
82
97
However, notice that the <c>is</c> operator returns <c>False</c> meaning that they are different objects (we will have more to say about this in a later chapter).</p>
<p>Notice that there are now 512 bananas—the dictionary has been modified. Note also that the <c>len</c> function also works on dictionaries. It returns the number
16
38
of key-value pairs:</p>
17
39
<p>
@@ -25,7 +47,7 @@
25
47
mydict = {"cat":12, "dog":6, "elephant":23}
26
48
mydict["mouse"] = mydict["cat"] + mydict["dog"]
27
49
print(mydict["mouse"])
28
-
</input>
50
+
</input>
29
51
</program>
30
52
</statement>
31
53
<choices>
@@ -35,31 +57,31 @@ print(mydict["mouse"])
35
57
</statement>
36
58
<feedback>
37
59
12 is associated with the key cat.
38
-
</feedback>
60
+
</feedback>
39
61
</choice>
40
62
<choice>
41
63
<statement>
42
64
<p>0</p>
43
65
</statement>
44
66
<feedback>
45
67
The key mouse will be associated with the sum of the two values.
46
-
</feedback>
68
+
</feedback>
47
69
</choice>
48
70
<choicecorrect="yes">
49
71
<statement>
50
72
<p>18</p>
51
73
</statement>
52
74
<feedback>
53
75
Yes, add the value for cat and the value for dog (12 + 6) and create a new entry for mouse.
54
-
</feedback>
76
+
</feedback>
55
77
</choice>
56
78
<choice>
57
79
<statement>
58
80
<p>Error, there is no entry with mouse as the key.</p>
59
81
</statement>
60
82
<feedback>
61
83
Since the new key is introduced on the left hand side of the assignment statement, a new key-value pair is added to the dictionary.
<p>The first assignment creates an empty dictionary named <c>eng2sp</c>. The other
18
27
assignments add new key-value pairs to the dictionary. The left hand side gives the dictionary and the key being associated. The right hand side gives the value being associated with that key.
19
28
We can print the current
@@ -26,20 +35,34 @@
26
35
For our purposes we can think of this ordering as unpredictable.</p>
27
36
<p>Another way to create a dictionary is to provide a list of key-value pairs
0 commit comments