What is the value of the __repr__ function over the normal function

I am trying to learn python with my own and I clicked on the __repr__ function. Although I read a lot of posts on __repr__ along with a python document. so I decided to ask this question here. The code below explains my confusion.

 class Point: def __init__(self,x,y): self.x, self.y = x,y def __repr__(self): return 'Point(x=%s, y=%s)'%(self.x, self.y) def print_class(self): return 'Point(x=%s, y=%s)'%(self.x, self.y) p = Point(1,2) print p print p.print_class() Point(x=1, y=2) Point(x=1, y=2) 

If a normal function can also perform a similar task, then what is the additional advantage of the __repr__ over print_class() function (in my case, a normal function).

+6
source share
3 answers

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 
+5
source

The difference is that the __repr__ function __repr__ automatically called by Python in certain contexts and is part of a predefined API with specific requirements. For example, if you enter p yourself (not print p ) into the interactive shell after creating your p object, its __repr__ will be called. It will also be used for print p if you do not define __str__ on p . (That is, you had to write print p.print_class() , but you did not need to write print p.__repr__() ; Python with the name __repr__ automatically for you.) The requirements for __repr__ described in the documentation :

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 ...>.

In short, if you write your own method called print_class , you can do whatever you want and tell people how to use it, because it is your API. If you use __repr__ , you must follow the Python API conventions. Anyone can make sense depending on the context.

+3
source

This helps you do more efficient coding work. even if you get the same result using a user definition method such as "print_class ()" like repr , but you do not need to enter ".print_class ()" in the repr method.

0
source

All Articles