repr()

Published Jun 2, 2023
Contribute to Docs

The repr() function returns a printable string describing the object that is passed in. In other words, it returns the string representation passed implicitly to the eval() function.

A class can control what this function returns for its instances when called by defining a __repr__() dunder method.

Syntax

The repr() function accepts a single object as parameter.

repr(object)

Codebyte Example

Below, two instances of repr() are called:

  • The first one returns the default value (a string with the name and address of the object, enclosed in angle brackets).
  • The second defines the output string and implements the __repr__()method.
us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn Python on Codecademy