Program 1

import math

class Vectory2(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(cls, P1, P2):
        return cls( 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
        
A = (10.0, 20.0)
B = (30.0, 35.0)
AB = Vectory2.from_points(A, B)

print "Vectory AB is", AB
print "Magnitude of Vector AB is", AB.get_magnitude()
AB.normalize()
print "Vector AB normalized is", AB

Program 2

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 two programs above I'M having trouble understanding the return values of the from_points functions.

In program 1 they are returned by an extra argument calles cls put in program to they are returned directly with the class name. Could someone please help me understand?
One more thing, in program 2 the __add__ function uses one of it's arguments "rhs" as with the variable x and y through dot notation and that's something I haven't seen before, nor does the book I'M reading explain it. Thanks.

In program 1, the '@classmethod' above the function definition indicates that it is a class method. You can call a class method by using the notation classname.method, without first instantiate a class object.
I think program 2 has the 'cls' missed out by mistake.

For __add__, this is used to overload the '+' operator. For doing addition, you need to add two operands, so the 'rhs' is just a class variable to hold the other operand, other than the self.

In program 1 they are returned by an extra argument calles cls put in program to they are returned directly with the class name. Could someone please help me understand?

Yes that what a class method do,did you read my link in the other post.
Vector2.from_points(A, C) here you call a method without making a instance(object).
Normal without @classmethod you make it like this.

v = Vector2(10, 20)   #make instance
v.from_points(5, 5)   #and call method like this.

You see the difference?
http://pyref.infogami.com/classmethod
http://docs.python.org/library/functions.html?highlight=classmethod#classmethod

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.