On Fri, 8 Jul 2022 at 02:22, Steve Jorgensen <stevecjor@gmail.com> wrote:
After some playing around, I figured out a pattern that works without any changes to the implementations of `dataclass` or `Enum`, and I like this because it keeps the 2 kinds of concern separate. Maybe I'll try submitting an MR to add an example like this to the documentation for `Enum`.

In [1]: from dataclasses import dataclass

In [2]: from enum import Enum

In [3]: @dataclass(frozen=True)
   ...: class CreatureDataMixin:
   ...:     size: str
   ...:     legs: int

In [4]: class Creature(CreatureDataMixin, Enum):
   ...:     BEETLE = ('small', 6)
   ...:     DOG = ('medium', 4)

In [5]: Creature.DOG
Out[5]: Creature(size='medium', legs=4)

I really like this example. I love dataclasses and I love enums and it looks like they go together like peanut butter and chocolate.You get a free initialiser (__init__ method) and all the other goodness of dataclasses (which are really very good).

I tweeted your example and it got 29 likes :-)



Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/G2VALQ4RIVFKIOKVW4XZAHZMLSZWL2XS/
Code of Conduct: http://python.org/psf/codeofconduct/

Michael Foord
Python Consultant, Contractor and Trainer