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
print('\tTest Failed: expected {} but got {}'.format(expected,actual))
23
23
return False
24
-
</input>
24
+
</code>
25
25
</program>
26
26
<p>To use this module when programming on your own computer, save the above code with the name <term>test.py</term> in the same folder as the python program you want to test.</p>
Copy file name to clipboardExpand all lines: pretext/ClassesBasics/AddingOtherMethodstoourClass.ptx
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@
15
15
<p>Let's add two simple methods to allow a point to give us information about its state. The <c>getX</c> method, when invoked, will return the value of the x coordinate. The implementation of this method is straight forward since we already know how
16
16
to write functions that return values. One thing to notice is that even though the <c>getX</c> method does not need any other parameter information to do its work, there is still one formal parameter, <c>self</c>. As we stated earlier, all methods defined in a class that operate on objects of that class will have <c>self</c> as their first parameter. Again, this serves as reference to the object itself which in turn gives access to the state data inside the object.</p>
""" Point class for representing and manipulating x,y coordinates. """
21
21
@@ -34,14 +34,14 @@ class Point:
34
34
p = Point(7, 6)
35
35
print(p.getX())
36
36
print(p.getY())
37
-
</input>
37
+
</code>
38
38
</program>
39
39
<p>Note that the <c>getX</c> method simply returns the value of <c>self.x</c> from the object itself. In other words, the implementation of the method is to go to the state of the object itself and get the value of <c>x</c>. Likewise, the <c>getY</c> method looks the same.</p>
40
40
<p>Let's add another method, <c>distanceFromOrigin</c>, to see better how methods
41
41
work. This method will again not need any additional information to do its work.
""" Point class for representing and manipulating x,y coordinates. """
12
-
12
+
13
13
def __init__(self, initX, initY):
14
14
""" Create a new point at the given coordinates. """
15
15
self.x = initX
16
16
self.y = initY
17
-
17
+
18
18
p = Point(7, 6)
19
-
</input>
19
+
</code>
20
20
</program>
21
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>
22
22
<imagesource="ClassesBasics/Figures/objectpic5.png"width="50%"alt="Simple object has state and methods"/>
<p><c>distance</c> takes two points and returns the distance between them. Note that <c>distance</c> is <term>not</term> a method of the Point class. You can see this by looking at the indentation pattern. It is not inside the class definition. The other way we
44
44
can know that <c>distance</c> is not a method of Point is that <c>self</c> is not included as a formal parameter. In addition, we do not invoke <c>distance</c> using the dot notation.</p>
0 commit comments