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~