Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question: Add a method sub (self, other) that overloads the subtraction operator

ID: 3619812 • Letter: Q

Question

Question: Add a method sub (self, other) that
overloads the subtraction operator, and try it out.
--
The reading for this question is posted below.
---
Operator overloading:

Some languages make it possible to change the definition of the built-in operators
when they are applied to user-defined types. This feature is called operator
overloading. It is especially useful when defining new mathematical types.
For example, to override the addition operator +, we provide a method named
_ _add_ _:
class Point:
   # previously defined methods here...
   def _ _add_ _(self, other):
      return Point(self.x + other.x, self.y + other.y)
As usual, the first parameter is the object on which the method is invoked. The
second parameter is conveniently named other to distinguish it from self. To
add two Points, we create and return a new Point that contains the sum of the
x coordinates and the sum of the y coordinates.
Now, when we apply the + operator to Point objects, Python invokes _ _add_ _:
>>> p1 = Point(3, 4)
>>> p2 = Point(5, 7)
>>> p3 = p1 + p2
>>> print p3
(8, 11)
The expression p1 + p2 is equivalent to p1. add (p2), but obviously more ele-
gant.
There are several ways to override the behavior of the multiplication operator: by
defining a method named _ _mul_ _ , or _ _rmul_ _ , or both.
If the left operand of * is a Point, Python invokes _ _mul_ _, which assumes that the
other operand is also a Point. It computes the dot product of the two points,
defined according to the rules of linear algebra:
def _ _mul_ _(self, other):
   return self.x * other.x + self.y * other.y
If the left operand of * is a primitive type and the right operand is a Point,
Python invokes rmul , which performs scalar multiplication:
def _ _rmul_ _(self, other):
   return Point(other * self.x, other * self.y)
The result is a new Point whose coordinates are a multiple of the original coor-
dinates. If other is a type that cannot be multiplied by a floating-point number,
then _ _rmul_ _ will yield an error.
This example demonstrates both kinds of multiplication:
>>> p1 = Point(3, 4)
>>> p2 = Point(5, 7)
>>> print p1 * p2
43
>>> print 2 * p2
(10, 14)
What happens if we try to evaluate p2 * 2? Since the first operand is a Point,
Python invokes _ _mul_ _ with 2 as the second argument. Inside _ _mul_ _ , the program
tries to access the x coordinate of other, which fails because an integer has no
attributes:
>>> print p2 * 2
AttributeError: ’int’ object has no attribute ’x’
Unfortunately, the error message is a bit opaque. This example demonstrates some
of the difficulties of object-oriented programming. Sometimes it is hard enough
just to figure out what code is running.
For a more complete example of operator overloading, see Appendix B. Question: Add a method sub (self, other) that
overloads the subtraction operator, and try it out.
--
The reading for this question is posted below.
---
Operator overloading:

Some languages make it possible to change the definition of the built-in operators
when they are applied to user-defined types. This feature is called operator
overloading. It is especially useful when defining new mathematical types.
For example, to override the addition operator +, we provide a method named
_ _add_ _:
class Point:
   # previously defined methods here...
   def _ _add_ _(self, other):
      return Point(self.x + other.x, self.y + other.y)
As usual, the first parameter is the object on which the method is invoked. The
second parameter is conveniently named other to distinguish it from self. To
add two Points, we create and return a new Point that contains the sum of the
x coordinates and the sum of the y coordinates.
Now, when we apply the + operator to Point objects, Python invokes _ _add_ _:
>>> p1 = Point(3, 4)
>>> p2 = Point(5, 7)
>>> p3 = p1 + p2
>>> print p3
(8, 11)
The expression p1 + p2 is equivalent to p1. add (p2), but obviously more ele-
gant.
There are several ways to override the behavior of the multiplication operator: by
defining a method named _ _mul_ _ , or _ _rmul_ _ , or both.
If the left operand of * is a Point, Python invokes _ _mul_ _, which assumes that the
other operand is also a Point. It computes the dot product of the two points,
defined according to the rules of linear algebra:
def _ _mul_ _(self, other):
   return self.x * other.x + self.y * other.y
If the left operand of * is a primitive type and the right operand is a Point,
Python invokes rmul , which performs scalar multiplication:
def _ _rmul_ _(self, other):
   return Point(other * self.x, other * self.y)
The result is a new Point whose coordinates are a multiple of the original coor-
dinates. If other is a type that cannot be multiplied by a floating-point number,
then _ _rmul_ _ will yield an error.
This example demonstrates both kinds of multiplication:
>>> p1 = Point(3, 4)
>>> p2 = Point(5, 7)
>>> print p1 * p2
43
>>> print 2 * p2
(10, 14)
What happens if we try to evaluate p2 * 2? Since the first operand is a Point,
Python invokes _ _mul_ _ with 2 as the second argument. Inside _ _mul_ _ , the program
tries to access the x coordinate of other, which fails because an integer has no
attributes:
>>> print p2 * 2
AttributeError: ’int’ object has no attribute ’x’
Unfortunately, the error message is a bit opaque. This example demonstrates some
of the difficulties of object-oriented programming. Sometimes it is hard enough
just to figure out what code is running.
For a more complete example of operator overloading, see Appendix B.

Explanation / Answer

Now, when we apply the - operator to Point objects, Python invokes __sub__:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote