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))
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)
That's what the self variable does. So when you are inside the definition of a class, you can use self to identify the object whose data you are trying to manipulate.
For example, suppose you have a class called human (which has a member variable named age), and every year, you want to increase the age of that human by calling the increment_age function. Then, you could write the following code:
class Human:
def __init__(self):
self.age = 0
def increment_age(self):
self.age += 1
>>> h = Human()
>>> print h.age
0
>>> h.increment_age()
>>> print h.age
1
So you see, by calling self, you are referring to the object itself. In your example, this would translate to self referring to point1.
Now, suppose that in the Human class, we want to add a function that allows two humans to fight. In this case, one human would have to fight another human (suppose that fighting another human increases your life by one and decreases the other human's life by one). In that case, you could write the following function within the Human class:
def fight(self, other_human):
self.age += 1
other_human.age -= 1
Now:
>>> h1 = Human()
>>> h2 = Human()
>>> h1.age = 5
>>> h2.age = 3
>>> print h1.age
5
>>> print h2.age
3
>>> h1.fight(h2)
>>> print h1.age
6
>>> print h2.age
2
Thus you can see in this example that h2 is the other_human in the fight function.
Hope that helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With