Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Magic Methods
3.
Python __repr__ Method
3.1.
Syntax
4.
Python __repr__ Implementation
4.1.
Python
4.2.
Python
5.
Frequently Asked Questions
5.1.
What are the methods in Python?
5.2.
How __repr__ can be used for logging and debugging?
5.3.
What are the special methods in Python?
5.4.
How __repr__ can be used for serializing and deserializing?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Python __repr__

Introduction

Python __repr__() is the built-in function that is used to represent a class’s objects and returns a string. Now this string can be used as an output or for printing to the user. In Python, there are magic methods, and __repr__ is one of them.

Python __repr__

In the article “Python __repr__”, we will discuss what are the magic methods in Python, what Python __repr__() does, along with its implementation.

Magic Methods

Magic Methods, also known as Dunder methods, contain two underscores in the prefix and suffix, and Dunder means Double Underscores. Few types of magic methods are: 

  • __init__() method: There are constructors in several languages such as C++, C#, and Java. Similarly, this method used for initialization without any call.
     
  • __add__() method: It is a magic method that is used to add two objects that returns the third object.
     
  • __len__() method: It is a magic method that is used to determine the length of objects passed to this method.
     
  • __repr__() method: This method is used to represent a class’s objects and returns a string

Python __repr__ Method

“__repr__” is one of the magic methods that represent the objects of the classes. Basically, the instance variables can be manipulated in the string, and this string can be returned, which can be used for printing out of this method.

Syntax

The syntax of Python __repr__ method:

obj.__repr__(self)

Python __repr__ Implementation

In this section of the article “Python __repr__”, we will see the implementation of the __repr__() method.

Example 1

In this example, we will implement __repr__ in python by creating a class “Car”.

Code

  • Python

Python

# Creating class "Car"
class Car:

   # Creating __init__ method to initialize the instance variables
   def __init__(self, name, model):
       self.name = name
       self.model = model
     
   # Creating __repr__ method to represent the object of class "Car"
   def __repr__(self):
       rep = 'Car(' + self.name + ', ' + self.model + ')'
       return rep

# Creating the objects "car1" and "car2" of class "Car"
car1 = Car("BMW", "i7")
car2 = Car("Mercedes", "S-Class")

# Representation of objects "car1" and "car2" by printing them using repr() method
print(repr(car1))
print(repr(car2))

 

Output

output 1

Explanation

In the above code, we created a class called “Car”, in which two magic methods are present. One magic method is the __init__() method, which will be called automatically for the initialization. The second magic method is __repr__() method which is called for the representation of the object of this class, there is a string returned as the result of this method. After that, two objects are created whose names are “car1” and “car2”, which is then passed to the repr() method, and the printing is done.

 

Example 2

Let's take one more example to understand better In this example, we will create a class called “College”, where we will represent its object using the __repr__ method. Here is the code for the same:

Code

  • Python

Python

# Creating class "College"

class College:

   # Creating __init__ method to initialize the instance variables

   def __init__(self, name, country, salary):

       self.name = name

       self.country = country

       self.salary = salary

   # Creating __repr__ method to represent the object of class "College"

   def __repr__(self):

       return str(self.name)+' | '+str(self.country)+' | '+str(self.salary)

# Creating the objects "clg1", "clg2" and "clg3" of class "Collge"

clg1 = College("IIT Delhi", "India", "17 LPA")

clg2 = College("IIT Bombay", "India", "20 LPA")

clg3 = College("IIM Bangalore", "India", "23 LPA")

# Representation of objects "car1", "car2" and "car3" by printing them using repr() method

print(repr(clg1))

print(repr(clg2))

print(repr(clg3))

 

Output

output 2

Explanation

In the above code, we created a class called “College”, in which two magic methods are present. One magic method is the __init__() method, which will be called automatically for the initialization. The second magic method is __repr__() method which is called for the representation of the object of this class, there is a string returned as the result of this method. After that, three objects are created whose names are “clg1”, “clg2”, and “clg3”, which is then passed to the repr() method, and the printing is done.

Frequently Asked Questions

What are the methods in Python?

A method in Python is a block of code that will only run when it is called. The data can also be passed in the method known as parameters, and the data can also be returned from the method.

How __repr__ can be used for logging and debugging?

If there are any errors in the application created using Python, then the __repr__ method can be used to print the error or meaningful message about the object.

What are the special methods in Python?

Special Methods are the methods that are not meant to be invoked directly by you. These methods contain two underscores in the prefix and suffix.

How __repr__ can be used for serializing and deserializing?

The __repr__ method can be used for converting objects to JSON or XML and converting them back to objects which are known as serializing and deserializing.

Conclusion

Magic Methods, also known as Dunder methods, contain two underscores in the prefix and suffix. Here __repr__ method is one of the magic methods that can be used to represent the class’s object. 

In this article, we discussed what are the magic methods, and we also discussed one of the magic methods called __repr__ along with its implementation. Here are more articles that are recommended to read:

You can also consider our paid courses such as DSA in Python to give your career an edge over others!

Happy Learning!

Live masterclass