In Python, __repr__ is a special method for representing the objects in a class as a string. The repr() built-in function calls __repr__. Using the __repr__ method, you can define your own string representation of your class objects. The Python repr() method is mostly used for the purpose of debugging and development. This article focuses on the concept of Python repr(), its syntax, various applications, and different examples.
Definition
- The Python repr() built-in function returns the printable representation of the specified object as a string.
- Python repr() function returns a printable representation of the object by converting that object to a string.
Python repr()
This string is intended to be the object’s representation and is useful for displaying to the programmer, for example, while working in an interactive interpreter. This frequently includes the object’s name, type and memory address by default, which indirectly assists the programmer in having information anytime it is required. This can be seen for custom class objects. An example of this can be seen below:
Example
class Student:
pass
s = Student()
print(repr(s))
Output
<__main__.Student object at 0x7f9193cbbd00>
A repr() is used to save pythonic code data in such a way that it can be extracted from the code using the library function. It calls the __repr__() method internally.
repr() Syntax
The syntax followed by Pythons repr() is:
repr(object)
repr() Parameters
The repr() function accepts a single parameter:
object – an object whose structured printable representation that must be returned
Return value from repr()
The Python repr() returns a printable representation of the object as a string.
Example 1: How repr() works in Python?
The code below demonstrates how to use the Python repr() inbuilt function to find the string representation of some basic Python objects:
# Python program to illustrate repr()
print(repr(repr))
txt = 'Hello'
print(repr(txt))
name = 'John'
print(repr(name))
list1 = ['Egg', 'Bread', 'Sauce']
print(repr(list1))
Output
<built-in function repr>
'Hello'
'John'
' ['Egg', 'Bread', 'Sauce'] '
In the above program, we have assigned the value ‘Hello’ to txt. The repr() function returns the value as “’Hello’”, ‘Hello’ inside double-quotes. The same goes for the other variables and their repr() return values.
Example 2: Implement __repr__() for custom objects
Python repr() function internally calls the __repr__() method of the given object. We can easily implement or override the __repr__() so that the repr() function works according to the programmer’s way.
Let us create a class Student, and define the __repr__() method. We will then call this method using the Python repr() built-in function.
Example
# Python program to illustrate repr() on custom objects
class Student:
name = 'Rocky'
age = 22
course = 'Masters'
def __repr__(self):
return repr('Hello ' + self.name + ' your age is ' + str(self.age) + ' and you have enrolled for ' + self.course)
s = Student()
print(repr(s))
Output
'Hello Rocky your age is 22 and you have enrolled for Masters'
Why should we use repr() in Python?
- Python repr() is simple to learn and useful in programs where the coder wants to troubleshoot the code. It is a useful function for extracting information from preconfigured libraries.
- The Python repr() function is mostly used for debugging and development.
- The repr() method is well-versed with CSV and data files, thus it may also operate well with binary texts.
- More than this repr() is understandable by the user, therefore the user generally understands what the correct function to do on his code is.
Frequently Asked Questions
Q1. What is Python __repr__?
In Python, __repr__ is a special method for representing the objects in a class as a string. The repr() built-in function calls __repr__. Using the __repr__ method, you can define your own string representation of your class objects.
Q2. How do I call a Python repr?
The Python repr() method is mostly used for the purpose of debugging and development. The Python repr() built-in function returns the printable representation of the specified object as a string.
The syntax followed by Pythons repr() is:
repr(object)
The repr() function accepts a single parameter:
object – an object whose structured printable representation that must be returned
Example
txt = 'How are you?'
print(repr(txt))
name = 'David'
print(repr(name))
Output
'How are you?'
'David'
Q3. What is the difference between str and repr in Python?
- Python repr() is used to generate the object’s “official” string representation (a representation that has all information about the object i.e. object type, name, memory address). Python str(), on the other hand, is used to compute an object’s “informal” string representation (a representation that is useful for printing the object).
- The print statement and str() built-in function utilizes __str__() method internally to display the object’s string representation, whereas the repr() built-in function uses __repr__().
- The str() function is used to generate a table of internal code differences. Python repr(), on the other hand, performs it by combining two routines from the same library.
- Python’s str() is used to generate output for the end-user, while the repr() is primarily used for debugging and development.
Q4. How do you print an object in a string in Python?
Python repr() function internally calls the __repr__() method of the given object. To print an object in a string using the Python repr() function, we use the following method:
Let us create a class Employee, and define the __repr__() method. We will then call this method using the Python repr() built-in function in order to print the object as a string.
Example
class Employee :
age='55'
def __repr__(self):
return repr(self.age)
a = Employee()
print(repr(a))
Output
'55'
Leave a Reply