17.5, 17.6: Constructers and Methods¶
17.5 Improving our Constructor¶
- Our constructor so far can only create points at location
(0,0) - To create a point at position (7, 6) requires that we 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.
- We can make our class constructor more general by putting extra parameters into the
__init__method:self.x = initX&self.y = inity
class Point:
""" Point class for representing and manipulating x,y coordinates. """
def __init__(self, initX, initY):
""" Create a new point at the given coordinates. """
self.x = initX
self.y = initY
p = Point(7, 6)
- Now when we create new points, we supply the x and y coordinates as parameters. When the point is created, the values of
initXand initYare assigned to the state of the object.
17.6 Adding other Methods (kinda functions) to our Class¶
- Rather than using a tuple for our points,
(7, 6), we can use thePointclass we made to add methods for operations on the points given - Creating a class like
Pointbrings an exceptional amount of “organizational power” to our programs, and to our thinking. - A method behaves like a function but it is invoked on a specific instance.
- For example, with a turtle named
tess,tess.right(90)asks thetessobject to perform itsrightmethod and turn 90 degrees - Methods are accessed using dot notation.
Example:¶
- The
getXmethod, when invoked, will return the value of the x coordinate.- The implementation of this method is straight forward since we already know how to write functions that return values
- Even though the
getX method does not need any other parameter information to do its work, there is still one formal parameter,self- All methods defined in a class that operate on objects of that class will have self as their first parameter.
class Point: """ Point class for representing and manipulating x,y coordinates. """ def __init__(self, initX, initY): """ Create a new point at the given coordinates. """ self.x = initX self.y = initY def getX(self): return self.x def getY(self): return self.y p = Point(7, 6) print(p.getX()) print(p.getY())
- All methods defined in a class that operate on objects of that class will have self as their first parameter.
Output:
7
6
-
Note that the
getX&getYmethod simply returns the value ofself.x&self.yfrom 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
x&y
- In other words, the implementation of the method is to go to the state of the object itself and get the value of
-
Example:
- Let’s add another method,
distanceFromOrigin -
This method will again not need any additional information to do its work.
class Point: """ Point class for representing and manipulating x,y coordinates. """ def __init__(self, initX, initY): """ Create a new point at the given coordinates. """ self.x = initX self.y = initY def getX(self): return self.x def getY(self): return self.y def distanceFromOrigin(self): return ((self.x ** 2) + (self.y ** 2)) ** 0.5 p = Point(7, 6) print(p.distanceFromOrigin())9.219544457292887
-
Notice that the caller of
distanceFromOrigindoes not explicitly supply an argument to match theselfparameter. - This is true of all method calls