[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-08 Thread Ethan Furman

On 7/7/22 09:01, Steve Jorgensen wrote:

> Actually, maybe these are fundamentally incompatible?

Their intended use seems fundamentally incompatible:

- dataclass was designed for making many mutable records (hundreds, thousands, 
or more)
- enum was designed to make a handful of named constants (I haven't yet seen 
one with even a hundred elements)

The repr from a combined dataclass/enum looks like a dataclass, giving no clue that the object is an enum, and omitting 
any information about which enum member it is and which enum it is from.


Given these conflicts of interest, I don't see any dataclass examples making it 
into the enum documentation.

--
~Ethan~
___
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/6CIPPTP2Z42GVUIVOUXZ7BW46DB5HWEZ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Make dataclass aware that it might be used with Enum

2022-07-08 Thread Barry Scott



> On 8 Jul 2022, at 02:22, Steve Jorgensen  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)

Can't you define the type of size as an enum?
Using multiple inheritance seems like the wrong way to go.
What if you are 10 fields in the dataclass that are all enums?
That could get messy.

Disclaimer I have not used dataclass. Just thinking from OOD point of view.

Barry

> ___
> 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/G2VALQ4RIVFKIOKVW4XZAHZMLSZWL2XS/
> Code of Conduct: http://python.org/psf/codeofconduct/
> 

___
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/CBH24S2XG5EF2CQWER6BWLBSZEGK4Q7O/
Code of Conduct: http://python.org/psf/codeofconduct/