[Python] Exposing a property setter/getter to dataclass repr/init

Discussão em 'Python' iniciado por Stack, Março 25, 2025.

  1. Stack

    Stack Membro Participativo

    With the introduction of python dataclasses you can declare your fields on the class and they will show up on the __init__ by default and you can customize it with field().

    Is there a clean way to add a property setter to the list of things that will show up in __init__? And the getter to the list of things that show up in __repr__?

    @dataclass
    class Foo:
    _a: int = field(init=False, repr=False)

    @property
    def a(self):
    return self._a

    @a.setter
    def a(self, value):
    self._a = value

    Continue reading...

Compartilhe esta Página