After all, what is the function of repr in python?

6

I'm a beginner in python and wanted to know what this really confusing function really is, I looked at several sites but none really tells what it's for.

    
asked by anonymous 01.05.2017 / 19:51

2 answers

4

__repr()__ serves to represent an object as a string . It does not have to be called directly. Whenever Python needs to resolve an object as string , __repr__() will be called automatically.

Multiple object types 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 as you see fit:

class MeuObjeto(object):
    def __init__(self):
        self.valor1 = 1
        self.valor2 = "2"

    def __repr__(self):
        return str(self.valor1) + ", " + self.valor2

Test:

>>> o = MeuObjeto()
>>> o
1, 2
    
01.05.2017 / 20:01
3

The repr calls the internal method __repr__ of the object. The idea is to return a string representation of any object - but rather think of the programmer rather 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 single quotation marks 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 quotation marks). 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 general rule, for simple, built-in objects, there was the way that if you copied the output of repr and pasted as text into a Python terminal or program, you would recreate the initial object back. This is true for strings, lists, dictionaries, tuples, and sets - and you can create% with% s of your own objects in a way that is worth it to them as well. But default%% shows the class name and instance ID (which in Python equals the memory address of the object, but this is an implementation detail): that is, you can not recreate the "repr" object by default, but you can distinguish one instance from another by looking at your% s of% s on the terminal.

Difference between repr and __repr__

Finally, it should be noted that every object in Python has a repr method - because oo __repr__ , the root of all objects in Python has one - and this method is called automatically in several situations: text form of an object for an interactive Python session, a text representation to be recursively included in a text representation of another object that contains the initial object, and some others.

The "built-in" repr method is the way to explicitly call the __repr__ of an object when we want to do this. Just as object can call the repr method and also __repr__ and len , __len__ and hash - the idea is that these built-in functions function practically as unary operators to access these features which objects have and which are written in the form of methods with "magic names" with two underscores. (In unofficial texts - such as emails, courses, and tutorials, you will see these methods being called "dunder" methods - a "double-underscore" contraction.)

And finally, when you request string representation of an object, with __hash__ , Python first looks for the next method - but unlike __next__ , not all objects have a method % default%. (The str class does not implement __str__ ) so the built-n __repr__ function does not find the __str__ calls the object of the object to have its representation as a string. This is one of the reasons why it is preferable to call built-in methods to call dunder methods directly - even if there is almost no difference between doing one thing and another for doing it.

In Python2 we had aggravation of type: method __str__ always had to return a string, not a unicode text. And the objects in addition to the str method could also implement __str__ . In addition,% w of strings transforms any non-ASCII character into an escape sequence of type "\ 0xPython 3 has greatly simplified all of this.

    
02.05.2017 / 00:54