In this Python tutorial, we will discuss the repr function. This function is actually available in every class as a member, by the name of __repr__. Calling this function returns a string that is a printable representation of that object.
Python repr function – Examples
Here are some examples of the repr function being used in Python.
myset = { 1, 2, 3 }
myinteger = 5
mystring = "Hello"
myfloat = 1.23
print(repr(myset))
print(repr(myinteger))
print(repr(mystring))
print(repr(myfloat))
{1, 2, 3}
5
'Hello'
1.23
Defining the __repr__ function
Normally when trying to print out objects of custom data types, we are met with a rather unreadable output. By defining the repr method in these custom Classes, we can help create more readable representations of Objects.
All datatypes such as integers, strings, floats etc. already have the __repr__ function defined for them. Hence using the repr()
function on them will work. When it comes to Custom Classes however, we need to define our own __repr__ methods.
Let’s create a simple Class called student and try printing it out.
class Student:
def __init__(self, name, age):
self.age = age
self.name = name
s1 = Student("John", 18)
print(s1)
<__main__.Student object at 0x000001F41746AF48>
As you can see, this is very unreadable and makes no sense to us. Now let’s define the repr function, and see what we can do about this problem.
class Student:
def __init__(self, name, age):
self.age = age
self.name = name
def __repr__(self):
return f"My Name is {self.name} and am {self.age} years old"
s1 = Student("John", 18)
print(repr(s1))
My Name is John and am 18 years old
Now the above code is much more readable and user friendly. You can customize the __repr__ method however you want.
Another interesting fact is, that if you have defined either the __str__ or __repr__ methods, you can directly call the print() function on them, and see the output.
print(s1) # produces the same output
My Name is John and am 18 years old
Using the repr function correctly
The repr function we just defined, was not a very great implementation. The actual purpose of the __repr__ function is meant to return a printable representation of the object. There is another similar function called __str__, whose purpose is to simply produce a nice readable string. Which is basically what we did with __repr__ in the last example.
So let’s just go through things once again. __str__ is used to produces nice user-friendly versions of the class object, where as __repr__ is meant to produce a code-friendly representation of the class object.
>>> import datetime
>>> today = datetime.datetime.now()
>>> str(today)
'2012-03-14 09:21:58.130922'
>>> repr(today)
'datetime.datetime(2012, 3, 14, 9, 21, 58, 130922)'
The above example should clear things up. The __str__ functions prints out a version that’s makes sense to a human, whereas repr produces a more code-friendly representation.
So let’s change our Student Class definition a bit, by rebranding the old __repr__ method as __str__ and define a new __repr__ function.
class Student:
def __init__(self, name, age):
self.age = age
self.name = name
def __str__(self):
return f"My Name is {self.name} and am {self.age} years old"
def __repr__(self):
return f"Student({self.name}, {self.age})"
s1 = Student("John", 18)
print(repr(s1))
print(str(s1))
Student(John, 18)
My Name is John and am 18 years old
This marks the end of the Python repr Function Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.