On 12/12/21 10:43 PM, Vioshim wrote:

> Anyways, at the moment that I write this message in python3.10.1, It happens that when making a class with the dataclasses module, this class can't actually be used in Multiple inheritance for Enum purposes, this is mostly to avoid code repetition by having to write the methods over again.
>
> Here's an example
>
> from dataclasses import dataclass
> from enum import Enum
>
> @dataclass
> class Foo:
>      a: int = 0
>
> class Entries(Foo, Enum):
>      ENTRY1 = Foo(1)
>      ENTRY2 = Foo(2)
>      ENTRY3 = Foo(3)
>
>
> assert Entries.ENTRY1.a == 1
>
> This raises AssertionError, due to the values not being defined at that point,

What do you mean by this?  Here is what I see:

--> E = Entries.ENTRY1
--> E
Entries(a=Foo(a=1))
--> E.value
Foo(a=1)
--> E.a
Foo(a=1)

Looks like both `value` and `a` are set.


You're problem is that you said Entries is a Foo, and then you set Enum/Foo instances to also have Foo values. Here's what you need:

class Entries(Foo, Enum):
    ENTRY1 = 1
    ENTRY2 = 2
    ENTRY3 = 3

Which gives us:

--> e = Entries.ENTRY2
--> e
Entries(a=2)
>>> e.value
2
--> e.a
2
--> isinstance(e, Foo)
True


--
~Ethan~
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/OZVCBTO4LSDV564FFBUPGRPU6U3PZZ6V/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to