repr()Advanced Examples

Returns a developer-friendly string representation of an object

repr() protocol implementation

Implementing the protocol that repr() uses under the hood.

python
# repr() - implementing the protocol
class Custom:
    def __repr__(self):
        return "custom result"

obj = Custom()
print(repr(obj))

Understanding the dunder methods that repr() calls helps you customize behavior for your own classes.

Edge cases with repr()

Handling unusual inputs and edge cases.

python
# repr() edge cases
print("Edge case handling for repr()")

# Type checking
print(type(42))

Knowing these edge cases prevents subtle bugs in production.

Want to try these examples interactively?

Open Advanced Playground