Skip to content

17.7, 17.8, 17.9: Objects, Strings, Return Values

17.7 Objects as Arguements and Parameters

  • You can use the object/classes as arguments in a functions
  • Function calling the Point object we created:

    def distance(point1, point2):
        xdiff = point2.getX() - point1.getX()
        ydiff = point2.getY() - point1.getY()
    
        dist = math.sqrt(xdiff**2 + ydiff**2)
        return dist
    
    p = Point(4, 3)
    q = Point(0, 0)
    print(distance(p, q))
    

    5.0

  • distance takes two points and returns the distance between them.

  • Note that distance is not a method of the Point class, it is a standalone function.

17.8 Converting an Object to a String

  • When we’re working with classes and objects, it is often necessary to print an object (that is to print the state of an object).

Example

  • The default print statement for an object is to tell you the type the object is.

    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)
    

    <main.Point object>

  • It does not tell you anything about the specific state of the point.

  • We can improve on this representation if we include a special method call __str__ in the Point object/class
  • _str__ method is responsible for returning a string representation as defined by the class creator.
  • You as the programmer, get to choose what a Point should look like when it gets printed.

    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
    
        def __str__(self):
            return "x=" + str(self.x) + ", y=" + str(self.y)
    
    p = Point(7, 6)
    print(p)
    

    x=7, y=6

17.9 Instances as Return Values

  • Functions and methods can return objects (Obviously)
  • Example: Have a point object and wish to find the midpoint halfway between it and some other target point.
  • Method, called halfway that takes another Point as a parameter and returns the Point that is halfway between the point and the target.

    class Point:
    
        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
    
        def __str__(self):
            return "x=" + str(self.x) + ", y=" + str(self.y)
    
        def halfway(self, 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)
    mid = p.halfway(q)
    
    print(mid)
    print(mid.getX())
    print(mid.getY())
    

    x=4.0, y=8.0 4.0 8.0