The __repr__ function __repr__ called repr() internally. repr() is called when you print the object directly, and the class does not define __str__() . From the documentation -
__ magnesia object __ (self)
Called by the built-in repr () function and string conversions (backticks) to compute the "official" string representation of an object. If at all possible, it should look like a valid Python expression that can be used to recreate an object with the same value (given the appropriate environment). If this is not possible, you must return the form string <... some useful description ...>. The return value must be a string. If a class defines __repr__() , but not __str__() , then __repr__() also used when an βinformalβ string representation of instances of this class is required.
In your case for print_class() , you must specifically call the method when printing the object. But in the case of __repr__() it gets an internally called print .
This is especially useful when you mix different classes / types. For example, you can take a list that can contain numbers and objects of your point class, now you want to print the elements of the list.
If you do not define __repr__() or __str__() , you need to check the instance first, whether its type is point , if it calls print_class() , or if it does not print the number directly.
But when your class defines __repr__() or __str__() , you can just call print on all elements of the list, the print statement will internally take care of printing the correct values.
Example. Suppose a class that has a print_class() method but not __repr__() or __str__() , code is
>>> class CA: ... def __init__(self,x): ... self.x = x ... def print_class(self): ... return self.x ... >>> l = [1,2,3,CA(4),CA(5)] >>> for i in l: ... print(i) ... 1 2 3 <__main__.CA object at 0x00590F10> <__main__.CA object at 0x005A5070> SyntaxError: invalid syntax >>> for i in l: ... if isinstance(i, CA): ... print(i.print_class()) ... else: ... print(i) ... 1 2 3 4 5
As you can see, when we mix numbers and objects of type CA in the list, and then when we just did print(i) , it did not print what we wanted. For this to work correctly, we had to check the type i and call the appropriate method (as was done in the second case).
Now let's assume a class that implements __repr__() instead of print_class() -
>>> class CA: ... def __init__(self,x): ... self.x = x ... def __repr__(self): ... return str(self.x) ... >>> >>> l = [1,2,3,CA(4),CA(5)] >>> for i in l: ... print(i) ... 1 2 3 4 5
As you can see in the second case, just printing worked, since print internally calls __str__() first, and since it wasnβt, it drops back to __repr__() .
And not only that, when we do str(list) , inside each element of the list __repr__() is called. Example -
The first case (without __repr__() ) -
>>> str(l) '[1, 2, 3, <__main__.CA object at 0x005AB3D0>, <__main__.CA object at 0x005AB410>]'
Second case (with __repr__() ) -
>>> str(l) '[1, 2, 3, 4, 5]'
In addition, in the interactive interpreter, when you directly use the object, it shows you the result of the repr() function, Example -
>>> class CA: ... def __repr__(self): ... return "CA instance" ... >>> >>> c = CA() >>> c CA instance