Closed lhoupert closed 3 years ago
You could declare the names
attribute as having a default value of None
(or really anything), then initialize them in __post_init__
. That will only help you at __init__
-time, though. There's no way to keep them in sync if positions is subsequently modified.
But why not just write a member function that returns what you want? I don't think these need to be fields of a dataclass.
I'm going to close this, since this repo is only for the 3.6 backport of dataclasses, which I'm not really supporting any more. You should probably ask this question on Stack Overflow or python-list for better answers.
Hi all, I was wondering if I missed this functionality or if it doesn't exist?
For example, I have this code:
How can I create additional attributes in the dataclass Section so they would be a list of the attribute of its subclass Position?
In my example, I would like that the section object also returns:
I was hoping to find a solution such as:
But of course, it is not working because
name
is not an attribute of position. I can define the object attributed later in the code (e.g. by doingsecs.name = [x.name for x in section.positions]
). But it would be nicer if it can be done at the dataclass definition level. Is it possible to do that?Thank you,
Loïc