Skip to main content
Last release Python version Test status Test coverage Last commit

reprfunc is a library that makes it easier to implement __repr__() for your classes. It implements a few common repr strategies (e.g. mimicking the contructor, getting values from a custom dunder method, displaying a hard-coded list of object attributes) and allows you use them simply by assigning to __repr__.

Installation

Install reprfunc from PyPI:

$ pip install reprfunc

Version numbers obey semantic versioning.

Examples

Make a repr-string that matches the arguments to the constructor:

>>> from reprfunc import *
>>> class MyObj:
...
...     def __init__(self, a, b):
...         self.a = a
...         self.b = b
...
...     __repr__ = repr_from_init
...
>>> MyObj(1, 2)
MyObj(a=1, b=2)

The same as above, but with variable positional and keyword arguments. These are handled as expected:

>>> class MyObj:
...
...     def __init__(self, *args, **kwargs):
...         self.args = args
...         self.kwargs = kwargs
...
...     __repr__ = repr_from_init
...
>>> MyObj(1, 2, a=3, b=4)
MyObj(1, 2, a=3, b=4)

The same as above, but demonstrating a variety of ways to control the output:

>>> class _MyObj:
...
...     def __init__(self, a, b, c, d=None, _state={}):
...         self.a = a
...         self._b = b
...         self.c = c
...         self.d = d
...         self._state = _state
...
...     __repr__ = repr_from_init(
...         # This option lets you change the class name at the beginning of
...         # the repr-string.
...         cls='MyObj',
...
...         # This option lets you explicitly map argument names to either
...         # attribute names, or callables that accept the object in
...         # question as their only argument.
...         attrs={'b': '_b'},
...
...         # These options allows you to exclude certain arguments from the
...         # repr-string.  The first is unconditional, and the second
...         # depends on the value of the given function.  By default,
...         # attributes with the same value as the default (like `d` in this
...         # example) will be skipped automatically.  Note that the
...         # predicate can be `True` to unconditionally include an argument,
...         # even if it still has its default value.
...         skip=['_state'],
...         predicates={'c': lambda self, x: x},
...
...         # This option allows you to specify that certain arguments should
...         # be rendered using the "positional" syntax.  Positional-only
...         # arguments are rendered this way by default.
...         positional=['a'],
...     )
>>> _MyObj(1, 2, 0, _state={3: 4})
MyObj(1, b=2)

Make a repr-string that gets its values from a __reprargs__() method defined by the object in question:

>>> class MyObj:
...
...     def __init__(self, a, b):
...         self.a = a
...         self.b = b
...
...     def __reprargs__(self):
...         # This function should return a list and a dictionary.  Any
...         # values in the list will be rendered as positional arugments,
...         # and any items in the dictionary will be rendered as keyword
...         # arguments.
...         return [self.a], {'b': self.b}
...
...     __repr__ = repr_from_dunder
...
>>> MyObj(1, 2)
MyObj(1, b=2)

Make a repr-string from a hard-coded list of attributes:

>>> class MyObj:
...
...     def __init__(self, a, b):
...         self.a = a
...         self.b = b
...
...     # Note that 'b' is specified twice here.  You can avoid this by
...     # specifying ``b=Key()``.
...     __repr__ = repr_from_attrs('a', b='b')
...
>>> MyObj(1, 2)
MyObj(1, b=2)

Use ReprBuilder to help formatting bespoke repr-strings. You can think of this class as a collection of positional and keyword arguments that knows how to format itself. It provides many more methods for registering positional/keyword arguments beyond what’s demonstrated here, so consult the source code if this seems useful:

>>> class MyObj:
...
...    def __init__(self, a, b):
...        self.a = a
...        self.b = b
...
...    def __repr__(self):
...        builder = ReprBuilder(self)
...        builder.add_positional_attr('a')
...        builder.add_keyword_attr('b')
...        return str(builder)
...
>>> MyObj(1, 2)
MyObj(1, b=2)

Alternatives

There are several other libraries out there that help with formatting repr-strings. Overall, the reason I wrote reprfunc was to make something more flexible and more succinct than the alternatives.

  • represent: This is a pretty similar library overall. The main difference is that it uses class decorators and/or inheritance to add its repr functions to your objects. One big advantage of this approach is that it allows “pretty-print” reprs for IPython to be added at the same time, but it also has a heavier feel.

  • reprutils: This is also a pretty similar library, but it only supports the equivalent of repr_from_attrs().

  • reprtools: This library doesn’t have much documentation, but seems to be mostly superseded by f-strings.

Metadata

Release files for reprfunc 0.2.0

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

Source distribution (sdist)

Source distribution for reprfunc 0.2.0
File Size Uploaded
reprfunc-0.2.0.tar.gz 13.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for reprfunc 0.2.0
File Interpreter ABI Platform
reprfunc-0.2.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 18.7 kB

Release files / reprfunc-0.2.0.tar.gz

Download URL reprfunc-0.2.0.tar.gz
Size 13.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7ef2df08375b0945a7b367fcc4416c93d5caaffa772cd151b8e7fa9dddc93516
BLAKE2b-256 checksum
How to use checksums
a974c13f83d67167769333ab770bf10a8e389915a77e4196a225f3374d4edf79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/35.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.4 keyring/23.5.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.13

Release files / reprfunc-0.2.0-py2.py3-none-any.whl

Download URL reprfunc-0.2.0-py2.py3-none-any.whl
Size 5.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
f39d33150df758afb34ae9b7c284835cb1023fa9d7e65a94fcc55854d9149138
BLAKE2b-256 checksum
How to use checksums
b348863ac53a1f34008b2d2de334b78ca5611908a69a29397b5e5c2f6b28782a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/35.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.4 keyring/23.5.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.13

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

0.0.0

2 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