Main.Overloading History

Hide minor edits - Show changes to markup

April 06, 2010, at 03:03 PM MST by 10.84.44.80 -
Changed line 92 from:

b = Person("John", "Cleese")]

to:

b = Person("John", "Cleese")

April 06, 2010, at 03:02 PM MST by 10.84.44.80 -
Added lines 1-95:

(:source lang=python:)

""" Operator overloading http://openbookproject.net/thinkcs/python/english2e/ch15.html#operator-overloading """

  1. We've seen that we can use the addition operator to "add" all kinds of things
  2. other than numbers, e.g. lists and strings.
  3. What does it mean to add Point objects?
  4. Recall that a point object has an x and y coordinates.
  5. Given two Point objects p1 and p2 we'd like their sum to be a point
  6. object whose x coordinate equals p1.x + p2.x and whose y coordinate
  7. equals p1.y + p2.y.
  8. We can do this by providing an __add__ methods as follows:

import math class Point :

    """Point is a class that stores a two dimensional point"""
    def __init__(self, x=0, y=0) :
        self.x = x
        self.y = y
    def __add__(self, other) :
        """add two Point objects"""
        return Point(self.x + other.x, self.y + other.y)
    def __repr__(self) :
        return "Point object with x = d" % (self.x, self.y)

p1 = Point(3, 4) p2 = Point(5, 7) p3 = p1 + p2 print p3

  1. Note that under the hood this is translated to something like:

p3 = p1.__add__(p2)

  1. You can similarly overload the subtratction operator.
  2. Overloading the multiplication operator is more interesting.
  3. First, what does it mean to multiply two Point objects? That is not obvious,
  4. and we could do it by defining the __mul__ method. This method assumes that
  5. the other operand is also a Point object.
  6. What about multiplying a Point object by a number? If the left operand is a
  7. primitive type you use __rmul__ :

import math class Point :

    """Point is a class that stores a two dimensional point"""
    def __init__(self, x=0, y=0) :
        self.x = x
        self.y = y
    def __add__(self, other) :
        """add two Point objects"""
        return Point(self.x + other.x, self.y + other.y)
    def __mul__(self, other) :
        return Point(other * self.x,  other * self.y)
    def __rmul__(self, other) :
        """multiply a Point object by a number"""
        return Point(other * self.x,  other * self.y)
    def __repr__(self) :
        return "Point object with x = d" % (self.x, self.y)

p = Point(3, 4) print p1 * 3 print 3 * p1

  1. By providing a __cmp__ function we can overload the comparison operator:

class Person (object) :

    """A class for storing information about a person"""
    def __init__(self, first_name, last_name) :
        self.first_name = first_name
        self.last_name = last_name
    def __cmp__(self, other) :
        print 'comparing'
        if self.last_name > other.last_name :
            return 1
        elif self.last_name < other.last_name :
            return 1
        if self.first_name > other.first_name :
            return 1
        elif self.first_name < other.first_name :
            return -1
        return 0
    def __repr__(self) :
        return "Person with Name = s" % (self.first_name, self.last_name)

a = Person("Asa", "Ben-Hur") b = Person("John", "Cleese")] a < b

(:sourceend:)