import math

class Vector2(object):
    
    def __init__(self, x = 0.0, y = 0.0):
        self.x = x
        self.y = y
        
    def __str__(self):
        return "(%s, %s)" % (self.x, self.y)
    
    @classmethod
    def from_points(P1, P2):
        return Vector2(P2[0] - P1[0], P2[1] - P1[1])
    
    def get_magnitude(self):
        return math.sqrt(self.x**2 + self.y**2)
    
    def normalize(self):
        magnitude = self.get_magnitude()
        self.x /= magnitude
        self.y /= magnitude
        
    # rhs stands for Right Hand Side
    def __add__(self, rhs):
        return Vector2(self.x + rhs.x, self.y + rhs.y)
    
A = (10.0, 20.0)
B = (30.0, 35.0)
C = (15.0, 45.0)
AB = Vector2.from_points(A, B)
BC = Vector2.from_points(B, C)

AC = Vector2.from_points(A, C)

print "Vector AC is", AC

AC = AB + BC
print "AB + BC is ", AC

In the code above I'M getting an error at the from_points function. It telling that it need 2 arguments but I've given it three. I can't find a third. There was also a program similar to this one where the from_points function returned an extra argument in it parameter called cls, why doesn't this one need what?
One last thing, could someone please explain the __add__ constructor to me? Thanks.

@classmethod
    def from_points(cls, P1, P2):
        return Vector2(P2[0] - P1[0], P2[1] - P1[1])

http://docs.python.org/library/functions.html?highlight=classmethod#classmethod

could someone please explain the __add__ constructor to me? Thanks.

>>> x = 5
>>> y = 6
>>> x + y
11
>>> dir(x)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']

So a simple explanation is to say that everything in Python is an object.
So x is an object and has method,and has a special method called __add__.
When + is used __add__ get called.
We can write it like this.

>>> x = 5
>>> y = 6
>>> x.__add__(y)
11
>>> 
>>> x.__add__.__doc__
'x.__add__(y) <==> x+y'
>>>

So we can say that special methods two underscore __something__ work in the background an get call when we as an example use * - + /.

Thanks, but what the hell is rhs.x and .y?

@classmethod
    def from_points(cls, P1, P2):
        return Vector2(P2[0] - P1[0], P2[1] - P1[1])

http://docs.python.org/library/functions.html?highlight=classmethod#classmethod

>>> x = 5
>>> y = 6
>>> x + y
11
>>> dir(x)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']

So a simple explanation is to say that everything in Python is an object.
So x is an object and has method,and has a special method called __add__.
When + is used __add__ get called.
We can write it like this.

>>> x = 5
>>> y = 6
>>> x.__add__(y)
11
>>> 
>>> x.__add__.__doc__
'x.__add__(y) <==> x+y'
>>>

So we can say that special methods two underscore __something__ work in the background an get call when we as an example use * - + /.

but what the hell is rhs.x and .y?

rhs is a name author of this code have choose.
You can can use whatever name you want when it comes to method/function arguments.
__add__ in this class will have an other meaing that normal __add__.
AB + BC call Vector2 and method def __add__(self, rhs)

# rhs stands for power of
def foo(rhs):
    a = 5
    b = a ** rhs
    return b

print foo(5) #3125

Or.
# my_car stands for power of
def foo(my_car):
    a = 5
    b = a ** my_car
    return b

print foo(5) #3125

So you see that you can use stupid name that confuse.

One more time, I've never seen dot notation in an argument.

rhs is a name author of this code have choose.
You can can use whatever name you want when it comes to method/function arguments.
__add__ in this class will have an other meaing that normal __add__.
AB + BC call Vector2 and method def __add__(self, rhs)

# rhs stands for power of
def foo(rhs):
    a = 5
    b = a ** rhs
    return b

print foo(5) #3125

Or.
# my_car stands for power of
def foo(my_car):
    a = 5
    b = a ** my_car
    return b

print foo(5) #3125

So you see that you can use stupid name that confuse.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.