what is the significance of `__repr__` function over normal function [duplicate]

2024/12/9 20:49:25

I am trying to learn python with my own and i stucked at __repr__ function. Though i have read lots of post on __repr__ along with the python document. so i have decided to ask this Question here. The code bellow explains my confusion.

class Point:def __init__(self,x,y):self.x, self.y = x,ydef __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 similar task then what is the extra advantage of __repr__ over print_class() (in my case a normal function) function.

Answer

The __repr__ function is called by repr() internally. repr() is called when you are printing the object directly , and the class does not define a __str__() . From documentation -

object.__repr__(self)

Called by the repr() built-in function and by string conversions (reverse quotes) to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <...some useful description...> should be returned. The return value must be a string object. If a class defines __repr__() but not __str__(), then __repr__() is also used when an “informal” string representation of instances of that class is required.

In your case for print_class() , you have to specifically call the method when printing the object. But in case of __repr__() , it gets internally called by print .

This is especially useful, when you are mixing different classes/types . For Example lets take a list which can have numbers and objects of your point class, now you want to print the elements of the list.

If you do not define the __repr__() or __str__() , you would have to first check the instance , whether its of type Point if so call print_class() , or if not directly print the number.

But when your class defines the __repr__() or __str__() , you can just directly call print on all the elements of the list, print statement would internally take care of printing the correct values.

Example , Lets assume a class which has print_class() method, but no __repr__() or __str__() , code -

>>> 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 of i and call the appropriate method (as done in second case).

Now lets 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 second case, simply printing worked, since print internally calls __str__() first, and as that did not exist fell back to __repr__() .

And not just this, when we do str(list) , internally each list's element's __repr__() is called. Example -

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]'

Also, in interactive interpreter, when you are directly using the object, it shows you the output of repr() function, Example -

>>> class CA:
...     def __repr__(self):
...             return "CA instance"
...
>>>
>>> c = CA()
>>> c
CA instance
https://en.xdnf.cn/q/73158.html

Related Q&A

Using celery with Flask app context gives Popped wrong app context. AssertionError

Im more or less using the setup to run Celery tasks using your flask app context from here: http://flask.pocoo.org/docs/0.10/patterns/celery/Im getting the same error message as Create, manage and kill…

How do I reduce the verbosity of chromedriver logs when running it under selenium?

My jenkins failure reports for my functional tests are full of lines like this:selenium.webdriver.remote.remote_connection: DEBUG: Finished Request selenium.webdriver.remote.remote_connection: DEBUG: P…

How to model python properties in UML diagram

What is a good practice to model Python properties in a UML class diagram? Properties themselves are class objects, their getter and setter are class functions. From Outside the class they look like i…

Linear regression with tensorflow

I trying to understand linear regression... here is script that I tried to understand: A linear regression learning algorithm example using TensorFlow library. Author: Aymeric Damien Project: https://g…

Are null bytes allowed in unicode strings in PostgreSQL via Python?

Are null bytes allowed in unicode strings?I dont ask about utf8, I mean the high level object representation of a unicode string.BackgroundWe store unicode strings containing null bytes via Python in …

Why the irrelevant code made a difference?

I am thinking to make a progress bar with python in terminal. First, I have to get the width(columns) of terminal window. In python 2.7, there is no standard library can do this on Windows. I know mayb…

What values to use for FastCGI maxrequests, maxspare, minspare, maxchildren?

Im running a Django app using FastCGI and lighttpd.Can somebody explain me what I should consider when deciding what value to use for maxrequests, maxspare, minspare, maxchildren?These options are not…

How to calculate the Silhouette Score for each cluster separately in python

You can easily extract the silhouette score with 1 line of code that averages the scores for all your clusters but how do you extract each of the intermediate scores from the scikit learn implementatio…

Can I tell python to put an existing figure in a new figure?

Creating a certain plot is a lot of work, so I would like to automate this by create a function f() that returns a figure.I would like to call this function so that I can put the result in a subplot. …

django-crispy-forms have field and button on same row

I am needing to have a bootstrap PrependedText field with a button on the same row. I can get it on the same row but it shows the button before the textbox and I want it after. What am I doing wrong an…