The official Python documentation says __repr__() is used to compute the “official” string representation of an object. The repr() built-in function uses __repr__() to display the object. __repr__() returns a printable representation of the object, one of the ways possible to create this object.
To put it simply: __str__ is used in to show a string representation of your object to be read easily by others. __repr__ is used to show a string representation of the object.
The __str__ method in Python represents the class objects as a string – it can be used for classes. The __str__ method should be defined in a way that is easy to read and outputs all the members of the class. This method is also used as a debugging tool when the members of a class need to be checked.
The __repr__ function is defined by the designer of a type, in order to provide a means for users of the type to represent values of that type unambiguously, with a string.
__repr__
should return a printable representation of the object, most likely one of the ways possible to create this object. See official documentation here. __repr__
is more for developers while __str__
is for end users.
A simple example:
>>> 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)
>>> p = Point(1, 2)
>>> p
Point(x=1, y=2)
This is explained quite well in the Python documentation:
repr(object): Return a string containing a printable representation of an object. This is the same value yielded by conversions (reverse quotes). It is sometimes useful to be able to access this operation as an ordinary function. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to
eval()
, otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a__repr__()
method.
So what you're seeing here is the default implementation of __repr__
, which is useful for serialization and debugging.
__repr__
is used by the standalone Python interpreter to display a class in printable format. Example:
~> python3.5
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class StackOverflowDemo:
... def __init__(self):
... pass
... def __repr__(self):
... return '<StackOverflow demo object __repr__>'
...
>>> demo = StackOverflowDemo()
>>> demo
<StackOverflow demo object __repr__>
In cases where a __str__
method is not defined in the class, it will call the __repr__
function in an attempt to create a printable representation.
>>> str(demo)
'<StackOverflow demo object __repr__>'
Additionally, print()
ing the class will call __str__
by default.
Documentation, if you please
The __repr__ method simply tells Python how to print objects of a class
An example to see the differences between them (I copied from this source),
>>> x=4
>>> repr(x)
'4'
>>> str(x)
'4'
>>> y='stringy'
>>> repr(y)
"'stringy'"
>>> str(y)
'stringy'
The returns of repr()
and str()
are identical for int x
, but there's a difference between the return values for str
y
-- one is formal and the other is informal. One of the most important differences between the formal and informal representations is that the default implementation of __repr__
for a str value can be called as an argument to eval, and the return value would be a valid string object, like this:
>>> repr(y)
"'a string'"
>>> y2=eval(repr(y))
>>> y==y2
True
If you try to call the return value of __str__
as an argument to eval, the result won't be valid.
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