The Python repr() function can be an extremely helpful tool for coders as it aids them in displaying the information in a human-readable format. It is helpful in a multitude of scenarios like debugging, development, and logging. The repr() function inspects the elements of objects like lists, sets, and even custom-made objects efficiently.
In this article, we will explore the functionality and significance of the repr() function while demonstrating it with the following content:
- Significance of repr() Function in Python
- How Does the Python repr() Function Work?
- How to Use repr() Function Using __repr__() for Custom Objects?
Significance of repr() Function in Python
- Python repr() function helps in the convenient “debugging, development and demonstration of the information” in string format.
- The repr() function is “easy to comprehend”, making it easier for the programmer to use it correctly in the code.
- The repr() function is helpful for “troubleshooting” code.
- It can get the information from “pre-configured libraries”.
- The repr() function works efficiently with “binary texts” due to its familiarity with “data and CSV files”.
How Does the Python repr() Function Work?
The repr() function accepts an object as an argument and returns a “printable representation of a provided object as a string”. It does so by converting the object to a string. It internally calls the “__repr__()” function.
Syntax
repr(object)
- The repr() function takes a parameter that is an “object” that is going to be a string
- The repr() function “returns a string” of the object in consideration.
Let’s get started with practical examples one by one.
Example 1: How to Get String Representation of a List Using repr() Function?
A list contains a collection of data. In Python, the repr() function can accept a List as an argument and retrieve the string representation of that particular list.
The following code demonstrates the functioning of repr() on lists:
my_list=[2,5,6,7]
list_repr=repr(my_list)
print(list_repr)
The above code illustrates:
- The initialized list contains the integers “2, 5, 6, and 7”.
- The parameter taken by the repr() function is a “list” named “my_list”.
- The result is displayed when list_repr is printed by calling the “print() function” with the parameter “list_repr”.
Output
The output of the above code is:
Example 2: How to Get String Representation of an Object Using the repr() Function?
Objects are variables that hold the information to perform manipulations. The function repr() can retrieve the string representation of the provided object by accepting the string object as a parameter.
The following code demonstrates the functioning of repr() on string objects:
str_object='I like to code in Python.'
str_repr=repr(str_object)
print(str_repr)
In the above code:
- The “str_object” is initialized with the value of the string object “I like to code in Python”.
- The repr() function is called with “str_object” as its parameter which is saved in “str_repr”.
- The result is displayed on the console by calling the “print()” function.
Output
The output of the above code is:
Example 3: How to Get String Representation of a Set Object Using the repr() Function?
Sets contain data that is unique, unordered, and mutable.. repr() can be used with a set object.
The following code demonstrates the functioning of repr() on set objects:
set_object={2,3,9,7}
set_repr=repr(set_object)
print(set_repr)
In the above code:
- A set “set_object” is created with the elements “2, 3, 9, and 7”.
- The repr() function has “set_object” as its parameter.
- The result is displayed in a printable representation by printing it using “print(set_repr)”.
Output
The output of the above code is:
How to Use repr() Function Using __repr__() for Custom Objects?
The repr() function internally calls the “__repr__()” function to get a printable representation of the object. The repr() function can be modified conveniently by “overriding” __repr__().
The following code demonstrates the functioning of repr() on custom objects:
class band:
name = 'Lauren'
age = 27
role = 'Producer'
def __repr__(self):
return repr('Hey, I am ' + self.name + '. I am ' + str(self.age) + ' and I am working as a ' + str(self.role) +' in my band.')
b = band()
print(repr(b))
In the above code:
- The repr() function is used on the custom objects, we have defined the class name “band” and given attributes “name“, “age”, and “role” to it.
- The __repr__() function is defined within the class.
- Name, age, and role are given the values of “Lauren”, “27” and “Producer” respectively.
- When the repr() function with the parameter self is called it takes the values of the attributes from the class “band” and produces the required result.
- The function “band()” is called which is saved in a variable ”b”. The output is displayed by using the print() function with “repr(b)” as its parameter.
- This implies that the “repr() function is called on b” which is then printed in a human-readable format.
Output
The output of the above code is:
Conclusion
The Python “repr()” function prints an “object as a string”. It saves the data, such that it can be extracted with the help of the library function. The function works by calling the __repr__() method internally. The Python function repr() inspects different kinds of objects so that they can be easily interpreted by the human. This Python tutorial provided a detailed guide on the repr() function, illustrating it with examples.