Python classes--Need explanation
Question: I have a general question on the class definition and its use..THe below code
from one of the book works fine but I have a general questions.

Here we have defined a class Point and creating 2 instance Point1 & Point2.
When calculating the distance for point2, how can we pass the point1 object?

Isn't point1 the point object, whereas the other_point is reprented as a
variable.

Im little confused.

Code:

    
    
    import math
    class Point:
        def move(self, x, y):
            self.x = x
            self.y = y
        def reset(self):
            self.move(0, 0)
        def calculate_distance(self, other_point):
            print("Inside calculating distance")
    
            return math.sqrt(
                    (self.x - other_point.x)**2 +
                    (self.y - other_point.y)**2)
    
    point1 = Point()
    point2 = Point()
    point1.reset()
    point2.move(5,0)
    print(point2.calculate_distance(point1))                    
    


Answer: When you create a `Point` object, several things happen.

    
    
    point1 = Point()
    point2 = Point()
    

One of the things that happens is that any methods belonging to the `Point`
class are _bound_. What this means is that one of the arguments in the method
is _fixed_ , so that it always refers to the instance created. Let's look at
the definition of `calculate_distance`.

    
    
    def calculate_distance(self, other_point):
        print("Inside calculating distance")
    
        return math.sqrt(
                (self.x - other_point.x)**2 +
                (self.y - other_point.y)**2)
    

You can probably guess which argument is fixed. When `Point()` is called and
an instance is created, the `self` parameter of `calculate_distnace` is fixed
so that it always refers to that instance. So whenever you do this:

    
    
    point1.calculate_distance(x)
    

You're doing the equivalent of this:

    
    
    Point.calculate_distance(point1, x)
    

And whenever you do this:

    
    
    point2.calculate_distance(point1)
    

You're doing the equivalent of this:

    
    
    Point.calculate_distance(point2, point1)
    

