1

how to overload the + operator for point class such that it works with either a point object or a tuple.

If the second operand is a Point, the method should return a new Point whose x coordinate is the sum of the x coordinates of the operands, and likewise for the y coordinates.

If the second operand is a tuple, the method should add the first element of the tuple to the x coordinate and the second element to the y coordinate, and return a new Point with the result.

so far I got nothing but point class which is:

class Point:
    def __init__(self, x, y):

       self.x = x
       self.y = y

I am still working on it and I am new to python so any type of idea would be a great help.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • (a) You could have Point extend tuple and then just use indexing. (b) numpy has excellent support for vectors, this will help if you ever start using matrix transformations on your points. (c) I would usually use a single complex number (e.g. `1 + 2j`) as a 2d point, although this depends on the application. – U2EF1 Jan 10 '14 at 03:34
  • Unrelated issue, but have `Point` inherit from `object` with `class Point(object):` and save yourself a few possible headaches down the line. – user2357112 Jan 10 '14 at 03:48

3 Answers3

5

Define __add__. Also define __radd__ if you want to allow tuple + Point.

>>> class Point:
...     def __init__(self, x, y):
...         self.x = x
...         self.y = y
...     def __add__(self, other):
...         if isinstance(other, Point):
...             return Point(self.x + other.x, self.y + other.y)
...         elif isinstance(other, tuple):
...             return Point(self.x + other[0], self.y + other[1])
...         raise TypeError
...     def __radd__(self, other):
...         return self + other
...
>>> p = Point(1, 2) + Point(3, 4)
>>> p.x
4
>>> p.y
6
>>> p2 = Point(1, 2) + (1, 1)
>>> p2.x
2
>>> p2.y
3
>>> p3 = (4, 0) + Point(1, 3)
>>> p3.x
5
>>> p3.y
3
>>> Point(1, 3) + 'x'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 10, in __add__
TypeError
falsetru
  • 357,413
  • 63
  • 732
  • 636
2

Just override the __add__ magic method:

def __add__(self, other):
    if isinstance(other, tuple):
         x, y = other #tuple unpacking
         return Point(self.x+x, self.y+y)
    elif isinstance(other, Point):
         x, y = other.x, other.y #Just for consistency
         return Point(self.x+x, self.y+y)
    else:
         raise TypeError("That data type is not supported")

Here is a little demo :)

>>> p = Point(10, 20) + Point(1, 2)
>>> p.x
11
>>> p.y
22
>>> p = Point(10, 20) + "asfbde"
Traceback (most recent call last):
  File "<pyshell#169>", line 1, in <module>
    p = Point(10, 20) + "asfbde"
  File "<pyshell#165>", line 14, in __add__
    raise TypeError("That data type is not supported")
TypeError: That data type is not supported

Hope this helps!

aIKid
  • 26,968
  • 4
  • 39
  • 65
0

You could try something like

 class Point:
    # ...
    def __add__(self, other):
        if type(other) is tuple:
            return Point(self.x+other[0], self.y + other[1])
        else:
            return Point(self.x+other.x, self.y+other.y)
MartinStettner
  • 28,719
  • 15
  • 79
  • 106