Skip to main content

reprshed

A toolshed for writing great __repr__ methods quickly and easily.

Replace boilerplate like this:

@reprlib.recursive_repr('<...>')
def __repr__(self):
    return f'{type(self).__name__}({self._foo!r}, bar={self._bar!r})'

with just this:

def __repr__(self):
    return reprshed.pure(self, self._foo, bar=self._bar)

Versioning

This library’s version numbers follow the SemVer 2.0.0 specification.

Installation

pip install reprshed

Usage

Import:

import reprshed

For a “pure” class (a class whose exact state can be recreated purely by passing the right constructor arguments), use reprshed.pure.

For an “impure” class (a class whose exact state is not reproducible), use reprshed.impure.

Examples!

>>> class MyClass:
...     def __init__(self, foo, bar, qux):
...         self._foo = foo
...         self._bar = bar
...         self._qux = qux
...     def __repr__(self):
...         return reprshed.pure(self, self._foo, self._bar, self._qux)

The first argument to reprshed.pure must be self, because reprshed uses that to get the class name. All other arguments are formatted as arguments to that class constructor:

>>> MyClass('foo', 'bar', qux='qux')
MyClass('foo', 'bar', 'qux')

Formatting the repr as constructor keyword arguments is easy too:

>>> class MyClass:
...     def __init__(self, foo, bar):
...         self._foo = foo
...         self._bar = bar
...     def __repr__(self):
...         return reprshed.pure(self, foo=self._foo, bar=self._bar)
...
>>> MyClass(1, bar='qux')
MyClass(foo=1, bar='qux')

You also get recursion protection automatically on all common Python implementations:

>>> class MyClass:
...     def __init__(self, number, foo):
...         self.number = number
...         self.foo = foo
...     def __repr__(self):
...         return reprshed.pure(self, self.number, foo=self.foo)
...
>>> my_instance = MyClass(1, 'whatever')
>>> my_instance
MyClass(1, foo='whatever')
>>> my_instance.foo = my_instance
>>> my_instance
MyClass(1, foo=<...>)
>>> my_instance.foo = MyClass(2, [None, my_instance, 3, '4'])
>>> my_instance
MyClass(1, foo=MyClass(2, foo=[None, <...>, 3, '4']))

Using reprshed.impure is the same, only the output format changes:

>>> class MyClass:
...     def __repr__(self):
...         return reprshed.impure(self, 1234, 'foo', bar=0, qux='qux')
...
>>> MyClass()
<MyClass 1234 'foo' bar=0 qux='qux'>

If you need to take more manual control, you can use reprshed.raw:

>>> class MyClass:
...     def __repr__(self):
...         return reprshed.impure(self, foo=5, bar=reprshed.raw('qux()'))
...
>>> MyClass()
<MyClass foo=5 bar=qux()>

By passing reprshed.raw as a positional argument, you can get arbitrary formatting inside the repr if you really need to:

>>> class MyClass:
...     def __repr__(self):
...         return reprshed.impure(self, reprshed.raw('a b () c,{d,e}'))
...
>>> MyClass()
<MyClass a b () c,{d,e}>

You can even use reprshed.raw to override the class name:

>>> class MyClass:
...     def __repr__(self):
...         return reprshed.impure(reprshed.raw('fake name'))
...
>>> MyClass()
<fake name>

Metadata

Release files for reprshed 1.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for reprshed 1.0.6
File Size Uploaded
reprshed-1.0.6.tar.gz 4.5 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for reprshed 1.0.6
File Interpreter ABI Platform
reprshed-1.0.6-py38-none-any.whl Python 3.8 none any Details
reprshed-1.0.6-py2.py30-none-any.whl Python 2, Python 3.0 none any Details

Total release size: 12.1 kB

Release files / reprshed-1.0.6.tar.gz

Download URL reprshed-1.0.6.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6381a081a268b7a958c35dd029c7d82447613af6c993f41095613ef2f93c01c2
BLAKE2b-256 checksum
How to use checksums
01aca0106f7f0363ede6afb3e4ef1b01fdb70ff94b61411bf70042674793c4d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.2

Release files / reprshed-1.0.6-py38-none-any.whl

Download URL reprshed-1.0.6-py38-none-any.whl
Size 3.8 kB
Tags Python 3.8
SHA-256 checksum
How to use checksums
f8049e900c85c9695dc8ce8c7d3c57a65d4e512ed1b61d851967dd2041b13a24
BLAKE2b-256 checksum
How to use checksums
6cf07393e094166c0d83c6ab705612a414774f4d898721ad2d801481d8a2929d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.2

Release files / reprshed-1.0.6-py2.py30-none-any.whl

Download URL reprshed-1.0.6-py2.py30-none-any.whl
Size 3.9 kB
Tags Python 2 Python 3.0
SHA-256 checksum
How to use checksums
0d6ec45c19366ead32eead81434326cebca576217fc112dc31cde5d1dfbd5e49
BLAKE2b-256 checksum
How to use checksums
b1194633ca9f331a96fc07411d415892a808a8ae5d56a62abcf72beb494ccb06
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.2

Release history Release notifications | RSS feed

This release

1.0.6 This release

3 release files

1.0.5

3 release files

1.0.4

3 release files

1.0.3

3 release files

1.0.2

3 release files

1.0.1

3 release files

1.0.0

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page