The line p2 = Points(1, 2)
creates a “Points” object with two attributes. The “x” attribute is set to 1, and the “y” attribute is set to 2.
The line p2.x = 2
changes the “x” attribute from its old value of 1 to the new value 2.
The line p2.print_point()
then calls the print_point method, which prints a message “x= VALUE y= VALUE” where the two values are taken from the object’s “x” and “y” attributes, which by this time are both 2.
This might be more clear to you if:
- you look at the object both before and after changing it;
- you use more distinctive values;
- and you compare two or more objects, not just one.
By the way, please format your code using “code fences”, or three
backticks on a line of their own, like this:
code goes between the code fences
Okay, let’s get back to your Points class.
That’s a poor name, because it is plural. Your class doesn’t represent more than one Point (Points, plural), it represents a single Point. So you should name it Point, not Points.
Once you have renamed your class to Point, let’s do this:
p = Point(10, 20)
p.print_point() # Should display x= 10 y= 20
p.x = 44
p.print_point() # Should display x= 44 y= 20
p.y = 15
p.print_point() # Should display x= 44 y= 15
Is that more clear what is going on? Now let’s try a second object.
q = Point(77, 99)
q.print_point() # Should display x= 77 y= 99
p.print_point() # Should display x= 44 y= 15
So p
and q
are different objects, each with their own pair of x and y attributes.
Hope this helps.