On Dec 11, 2019, at 15:10, Ricky Teachey <ri...@teachey.org> wrote:
>
>
> 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}"
Well, you could always do this:
assert value == f”{self.name} {self.surname}”
self._fullname = value
Now, the property is “frozen” in that you can legally mutate it to what it
already is, but not to anything else. But that’s pretty weird semantics. (And
the fact that breaking the rule means an assertion error rather than the usual
one is even weirder, but that part is easy to fix.)
I think what you’re really looking for here is the C++ feature of being able to
declare a attribute as mutable even in const instances. Then you could use it
to cache your fullname just by assigning to the always-mutable but private
_fullname attribute, and have a readonly fullname property that uses that cache.
_______________________________________________
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/AWMDTDOZ3E44YYO4MMPP6MRWGADPYQD7/
Code of Conduct: http://python.org/psf/codeofconduct/