bah, I forgot the underscore in the setter. Corrected for clarity: @dataclasses.dataclass(frozen=True) class Person: name: str surname: str fullname: str = dataclasses.field(property=True) # fullname is now a field descriptor
@fullname.getter def fullname_getter(self): return self._fullname @fullname.setter def fullname_setter(self, value): assert value == ???? self._fullname = f"{self.name} {self.surname}" --- Ricky. "I've never met a Kentucky man who wasn't either thinking about going home or actually going home." - Happy Chandler On Wed, Dec 11, 2019 at 6:08 PM Ricky Teachey <ri...@teachey.org> wrote: > > >> I agree with Andrew: it's a great idea, but I couldn't come up with a >> way to implement it, so I followed attrs lead instead. Maybe someone can >> come up with a good way to implement it > > > It seems like this could be solved if a general way could be created to > allow dataclasses to handle descriptors properly? Or if not descriptors > proper, at least a dataclass version of @property? > > I'm sure this has been discussed before. But one idea would be to add an > optional "property" argument to `field`, which would turn it into a > descriptor. > > So the code could be something like this, maybe: > > @dataclasses.dataclass(frozen=True) > class Person: > name: str > surname: str > fullname: str = dataclasses.field(property=True) # fullname is now a > field descriptor > > @fullname.getter > def fullname_getter(self): > return self._fullname > > @fullname.setter > def fullname_setter(self, value): > assert value == ???? > self.fullname = f"{self.name} {self.surname}" > > If this is a good idea, one big question is what the default __init__ > method should supply to the "value" argument. >
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/OWM5POF7U3UWWST427STKGROOYB2QUKW/ Code of Conduct: http://python.org/psf/codeofconduct/