liveBook · Manning

__repr__

__repr__ is a Python special method that returns an object’s developer-oriented string representation, ideally unambiguous and suitable for recreating an equivalent object.

How 3 Manning books explain it

Python Workout: 50 ten-minute exercises

A Python special method that returns an object's developer-oriented string representation, conventionally one that could represent the object in valid Python syntax.

How it is used
Formats objects for developers and makes printed containers reveal their contents.
How deep it goes
Defines the developer-facing convention and demonstrates inheritance-based implementations, while deferring distinct end-user formatting to __str__.
Examples
Uses colored animal objects, inherited representations, scoop collections, and cages containing multiple animals.
What you can do after
Apply it via Python special methods to format custom objects and reuse representations across an inheritance hierarchy.

Read how Python Workout: 50 ten-minute exercises explains __repr__ →

Python How-To

__repr__ is a Python special method that returns an official, unambiguous string representation of an object, ideally one that can recreate an equivalent object when evaluated as code.

How it is used
Uses __repr__ for developer-facing debugging and interactive-console inspection.
How deep it goes
Explains implementation, invocation, string quoting, and reconstruction, while deferring broader object-model internals.
Examples
Demonstrates Task representations, eval(repr(task)), a quoting failure, and dataclass-generated representations.
What you can do after
Build custom classes with inspectable, constructor-like representations and diagnose invalid output caused by missing string quotes.

Read how Python How-To explains __repr__ →

Python Workout, Second Edition

__repr__ is Python's special method for producing an object's developer-oriented string representation, conventionally one that is unambiguous and potentially valid Python syntax.

How it is used
Formats objects for developer-oriented output and reports containing objects through print().
How deep it goes
Explains representation roles, generator-based formatting, and nested reports, but stops short of implementation internals.
Examples
Uses bowls of scoops, cages of animals, and inherited animal representations as concrete exercises.
What you can do after
Apply it via Python special methods to produce useful representations for objects and containers.

Read how Python Workout, Second Edition explains __repr__ →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.