[Python] Overiding __mul__ in two dimensional vector class to preserve commutivity

Discussão em 'Python' iniciado por Stack, Dezembro 31, 2024.

  1. Stack

    Stack Membro Participativo

    I have defined the following class:

    class Point(object):

    def __repr__(self):
    return "("+str(self.x)+","+str(self.y)+")"

    def __init__(self, x, y):
    self.x = x
    self.y = y

    def __add__(self, point):
    return Point(self.x+point.x,self.y+point.y)

    def __sub__(self, point):
    return Point(self.x-point.x,self.y-point.y)

    def __mul__(self, num):
    return Point(num*self.x,num*self.y)

    def length(self):
    return (self.x**2 + self.y**2)**.5


    And the following code works:

    x = Point(1,2)
    y = Point(1,3)
    print x+y
    print (y+x)
    print (x-y)
    print (y-x)
    print y*3


    with output:

    (2,5)
    (2,5)
    (0,-1)
    (0,1)
    (3,9)


    But this does not:

    print 3*y


    It gives the following error:

    ----> 1 print 3*y
    TypeError: unsupported operand type(s) for *: 'int' and 'Point'


    Which is because the point class is being fed into the int mul function, I'm assuming. How can I keep the definition of point contained in the Point class and still have 3*y return the same as y*3?

    Continue reading...

Compartilhe esta Página