On 12/13/2021 1:43 AM, Vioshim wrote:
Hello good morning.
I've decided to open a discussion of a topic that I consider should be part of
dataclasses, but not sure how suggestions work and many threads recommend to
check python dev first so-.
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,
as the class is currently just using the default paremeters. This is why I
think it'd be essential to implement __set__ in the module.
I don't understand what you're trying to achieve.
Entries.ENTRY1.a is Foo object. Why would you expect it to be 1? And I
don't understand why you're inheriting from Foo. What are you trying to
accomplish with that?
Could you explain in more detail why you're using this pattern?
Eric
Currently there's a workaround to have this work, which is defining a __set__
but doing this, in more complex cases, can get to be very tedious and
repetitive coding wise
from dataclasses import dataclass
from enum import Enum
@dataclass
class Foo:
a: int = 0
def __set__(self, instance, value: int):
instance.a = value
class Entries(Foo, Enum):
ENTRY1 = Foo(1)
ENTRY2 = Foo(2)
ENTRY3 = Foo(3)
assert Entries.ENTRY1.a == 1
Have a great day, and hopefully this post enables a good discussion.
_______________________________________________
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/U2YDIU5T67GFV4PAOHBMOGURG6TYUHP7/
Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
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/P64KFE6VITRI74FCFTR7KAAP7G25MVEJ/
Code of Conduct: http://python.org/psf/codeofconduct/