A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://realpython.com/ref/builtin-functions/repr/ below:

Python’s Built-in Functions – Real Python

The built-in repr() function provides a developer-friendly string representation of an object. This string aims to be unambiguous and, ideally, should be able to recreate the object using the representation as an argument to the eval() function:

repr() Signature Arguments Argument Description object The object to represent Return Value repr() Examples

With a simple integer as an argument:

With a list as an argument:

repr() Common Use Cases

The most common use cases for the repr() function include:

repr() Real-World Example

Suppose you’re developing a custom class to represent a Person. You want to provide a clear, developer-friendly representation of each instance:

In this example, repr() provides a clear and detailed representation of the Person object, which can be used to recreate the object.

repr() in Custom Classes

To support repr() in your custom classes, you can define the .__repr__() special method. Here’s how you can implement it:

The .__repr__() method allows instances of the Book class to provide a detailed and developer-friendly string representation, which is useful for debugging and logging.

For additional information on related topics, take a look at the following resources:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4