English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Fonctions en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced Python knowledge

Python reference manual

Python repr() usage and examples

Python built-in functions

The repr() function returns the printable representation form of the given object.

The syntax of repr() is:

repr(obj)

repr() parameters

repr() function takes a single parameter:

  • obj -Must return the object in its printable representation form

repr() return value

The repr() function returns a printable string representation of the given object.

Example1: How repr() works in Python?

var = 'foo'
print(repr(var))

Output result

"'foo'"

Here, we providevarAssign a value 'foo'. Then, the repr() function returns "'foo'", with 'foo' inside the double quotes.

When the result of repr() is passed to eval(), we will obtain the original object (for many types).

>>> eval(repr(var))
'foo'

Example2: Implement __repr__() for custom objects

Internally, the repr() function calls the given object's __repr__().

You can easily implement/Rewriting __repr__() and the different ways repr() works.

class Person:
    name = 'Adam'
    def __repr__(self):
        return repr('Hello ' + self.name )
print(repr(Person()))

Python built-in functions