[Python-Dev] Dataclasses, frozen and __post_init__
Eric V. Smith
eric at trueblade.com
Tue Feb 20 17:41:15 EST 2018
> On Feb 20, 2018, at 5:38 PM, Guido van Rossum <guido at python.org> wrote:
>
>> On Tue, Feb 20, 2018 at 1:37 PM, Eric V. Smith <eric at trueblade.com> wrote:
>>> On 2/17/2018 2:35 PM, Guido van Rossum wrote:
>>> PS. I have to ponder why frozen dataclasses don't use `__new__`.
>>
>> As I'm sure everyone is now aware after the rest of this discussion: it's because the returned object isn't really immutable.
>>
>> That said, I have threatened to create a decorator similar to typing.NamedTuple that has the @dataclass features (except maybe __post_init__) and returns a class that does inherit from tuple, in which case it really would use `__new__`. I'll save that for 3.8, if it ever happens. There's a lot there to think about, first. For now, typing.NamedTuple is the way to go if you want something based on namedtuple yet using type hints.
>
> But then the class would also inherit a bunch of misfeatures from tuple (like being indexable and having a length).
Right. That’s the “lots to think about” part. It might just be unworkable.
> It would be nicer if it used __slots__ instead. (Also, the problem with __slots__ is the same as the problem with inheriting from tuple, and it should just be solved right, somehow.)
Agreed.
Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180220/9d832240/attachment.html>
More information about the Python-Dev
mailing list