Core Library0.xAPI Reference

Choose documentation 3

latest

Current version

latest
English

Early release libtmux is released, and its APIs may still change between releases. This site's structure and URLs may change too.

class libtmux latest

SkipDefaultFieldsReprMixin

Skip default fields in dataclass object representation.

Source and package
Qualified name
libtmux._internal.dataclasses.SkipDefaultFieldsReprMixin
Module
libtmux._internal.dataclasses
Package
libtmux
Source
src/libtmux/_internal/dataclasses.py
class libtmux._internal.dataclasses.SkipDefaultFieldsReprMixin

Description#

Notes

Credit: Pietro Oldrati, 2022-05-08, Unilicense

https://stackoverflow.com/a/72161437/1396928

Examples#

>>> @dataclasses.dataclass()
... class Item:
... name: str
... unit_price: float = 1.00
... quantity_on_hand: int = 0
...
>>> @dataclasses.dataclass(repr=False)
... class ItemWithMixin(SkipDefaultFieldsReprMixin):
... name: str
... unit_price: float = 1.00
... quantity_on_hand: int = 0
...
>>> Item('Test')
Item(name='Test', unit_price=1.0, quantity_on_hand=0)
>>> ItemWithMixin('Test')
ItemWithMixin(name=Test)
>>> Item('Test', quantity_on_hand=2)
Item(name='Test', unit_price=1.0, quantity_on_hand=2)
>>> ItemWithMixin('Test', quantity_on_hand=2)
ItemWithMixin(name=Test, quantity_on_hand=2)

If you want to copy/paste the __repr__() directly, you can omit the repr=False:

>>> @dataclasses.dataclass
... class ItemWithMixin(SkipDefaultFieldsReprMixin):
... name: str
... unit_price: float = 1.00
... quantity_on_hand: int = 0
... __repr__ = SkipDefaultFieldsReprMixin.__repr__
...
>>> ItemWithMixin('Test')
ItemWithMixin(name=Test)
>>> ItemWithMixin('Test', unit_price=2.00)
ItemWithMixin(name=Test, unit_price=2.0)
>>> item = ItemWithMixin('Test')
>>> item.unit_price = 2.05
>>> item
ItemWithMixin(name=Test, unit_price=2.05)

Members1#

  • __repr__ method Omit default fields in object representation.
Esc

Type to search.

Reference previewOpen reference