represent.core#

Note

Names in this module should be imported from represent. They are in represent.core for structural reasons.

class represent.core.ReprHelperMixin[source]#

Bases: object

Mixin to provide __repr__ and _repr_pretty_ for IPython.lib.pretty from user defined _repr_helper_ function.

For full API, see represent.helper.BaseReprHelper.

def _repr_helper_(self, r):
    r.positional_from_attr('attrname')
    r.positional_with_value(value)
    r.keyword_from_attr('attrname')
    r.keyword_from_attr('keyword', 'attrname')
    r.keyword_with_value('keyword', value)

New in version 1.3.

represent.core.autorepr(*args, **kwargs)[source]#

Class decorator to construct __repr__ automatically based on the arguments to __init__.

_repr_pretty_ for IPython.lib.pretty is constructed unless include_pretty=False.

__rich_repr__ for rich.pretty is constructed unless include_rich=False.

Parameters:
  • positional – Mark arguments as positional by number, or a list of argument names.

  • include_pretty – Add a _repr_pretty_ to the class (defaults to True).

  • include_rich – Add a __rich_repr__ to the class (defaults to True).

Example:

>>> @autorepr
... class A:
...     def __init__(self, a, b):
...         self.a = a
...         self.b = b

>>> print(A(1, 2))
A(a=1, b=2)
>>> @autorepr(positional=1)
... class B:
...     def __init__(self, a, b):
...         self.a = a
...         self.b = b

>>> print(A(1, 2))
A(1, b=2)

New in version 1.5.0.