What is the use of `__repr__`?

in the: Lesson

What is the use of the dunder __repr__?

class Employee():
  def __init__(self, name):
    self.name = name

  def __repr__(self):
    return self.name

argus = Employee("Argus Filch")
print(argus)
# prints "Argus Filch"

Only, that I can access the object attributes directly, after the object initiation?

instead, I can use:
either:

def repr(self):
  return self.name.

and then, call outside the class:
print(argus.repr())
else:
print(argus.name)

The thing about dunder methods is that you can call them using something other than the method name. for instance,__add__(self, y) can be called by an object, say argus, as argus + y, and you can thus define what the + operator does in your class.

In the case of __repr__(), you can control what is printed if the user types print(argus), which is one way the method can be called: no dot notation required.

13 Likes

Thank you. I didn’t completely understand what advantages does it contain. Hope, to do it in the future.

10 Likes

Does all the dunder methods run when an object is called? no right? because we didn’t call the repr method , but string is printed.

here is a simple explanation of repr and its role :

class CodLearnin:
    def __init__(self,platform):
        self.platform = platform + "is the best ever"
        print(self.platform)
message = CodLearnin("Codecademy ")

you see now that we use print before the message, and you can only write inside of it: self. platform in order to get the message.
But, how to call the instance variable which is : message? try to run this code:

class CodLearnin:
    def __init__(self,platform):
        self.platform = platform + "is the best ever"
        print(self.platform)
message = CodLearnin("Codecademy ")
print(message)

Now you see two outputs: Codecademy is the best ever
<main.CodLearnin object at 0x000001E49D887400>
So, you can’t call message(instance variable), and our hero repr will absolutely help you, it can make you call your instance variables without writing print before, we substitute print(self. platform) with

    def __repr__(self):
        return self.platform

Your example doesn’t really fit the purpose of this method; print(message.platform) after the instance is created would perform the task you described perfectly well.

The role of __repr__ is to create a formal string representation of the object that should ideally “look like a valid Python expression that could be used to recreate an object”. This should really differ from an informal representation like this.

# In this example the output of this call should be something like-
print(repr(message))
Out: CodLearnin("Codecademy ")

The docs linked describe how the two differ-

The __repr__ method is mainly used for development and debugging-
https://docs.python.org/3/reference/datamodel.html?emulating-container-types#object.repr

For an informal representation or more human readable string representation the __str__ method is suggested-
https://docs.python.org/3/reference/datamodel.html?emulating-container-types#object.str

Both these methods are really designed to represent the object itself, for printing out individual attributes then just do that. When the __str__ method is defined it is used first when calling print(obj) and __repr__ is the fallback when it is not used.

1 Like