11
I’m a bit of a beginner in python and I wanted to know what this confusing function is really for, I looked at several sites but no one has really said what it’s for
11
I’m a bit of a beginner in python and I wanted to know what this confusing function is really for, I looked at several sites but no one has really said what it’s for
10
__repr()__
serves to represent an object as a string
. It does not need to be called directly. Every time Python needs to solve an object like string
, __repr__()
will be called automatically.
Various types of objects implement __repr__()
by default. For objects, usually __repr__()
returns the description and memory reference for the object.
The great advantage of __repr__()
is just being able to rewrite it, as I did here. Instead of writing the object as <tipo + referência>
, you can format your string
in whatever way:
class MeuObjeto(object):
def __init__(self):
self.valor1 = 1
self.valor2 = "2"
def __repr__(self):
return str(self.valor1) + ", " + self.valor2
Testing:
>>> o = MeuObjeto()
>>> o
1, 2
Thank you, you helped me a lot!
5
The repr
calls for the internal method __repr__
object. Its idea is to return a string representation of any object - but thinking more about the programmer than the end user of the program. Above all it is the repr
that is called for each expression result when we use Python in interactive mode.
So, for example, for a string in a variable a
, repr(a)
returns the string, but with simple quotes around it. In contrast, str(a)
shows the object as a string, but in a form that is user-friendly, when the program is running. (That is, the contents of the string, without the quotes). See the difference:
>>> a = "maçã"
>>> a # internamente, o Python chama o "a.__repr__"
'maçã'
>>> print(a) # internamente, o Python (o código no "print") chama "a.__str__"
maçã
As a rule, for simple, built-in objects, there was the idea that if you copied the output of the repr
and paste as text into a Python terminal or program, you would recreate the initial object back. This goes for strings, lists, dictionaries, tuples and sets - and you can create repr
of their own objects in a way that also counts for them. But the __repr__
pattern shows the class name and the instance ID (which in Python equals the memory address of the object, but this is an implementation detail): that is, it is not possible to recreate the object "Repr" by default, but one can distinguish one instance from another by looking at its repr
s in the terminal.
Difference between __repr__
and repr
Finally, it is worth noting that every Python object has a method __repr__
- why the object
, root of all objects in Python has one - and this method is automatically called in several situations: to have a text form of an object for an interactive Python session, a text representation to be recursively included in a text representation of another object, which contains the initial object, and some others.
Already the method "built-in" repr
is the way of explicitly calling __repr__
of an object when we want to do this. As well as the len
can call the method __len__
and also to hash
and __hash__
, next
and __next__
- the idea is that these built-in functions work practically as unary operators to access these functionalities that objects have and that are written in the form of methods with the "magic names", with two underscores. (In unofficial texts - such as emails, courses, and tutors, you will see these methods being called "Dunder" methods - a contraction of "double-underscore".)
And finally, it is worth saying that when the representation as a string of an object is requested, with str
, Python first searches the method __str__
- but unlike the __repr__
not all objects have a method __str__
standard. (The class object
does not implement __str__
) then built-n function str
by not finding the __str__
calls the __repr__
of the object to have its representation as string. This is one of the reasons why it is preferable to call the built-in methods to call the "Dunder" methods directly - even if for Repr there is almost no difference between doing one thing and another.
In Python2 we had aggravating agents of the type: the method __repr__
always had to return a string, not a Unicode text. And the objects, in addition to the method __str__
could also implement the __unicode__
. In addition, the repr
of strings transforming any non-ASCII character into an escape sequence of type " 0xPython 3 greatly simplified all this.
thanks teacher! , I could only see now, thank you!
Browser other questions tagged python function
You are not signed in. Login or sign up in order to post.
An easy way to remember:
print()
is for the user,repr()
is for the programmer.– dot.Py