Just to note, ** is not an operator, but part of the function-call syntax, so this only works for unpacking a mapping-valued function argument or element in a dict display, not as a stand-alone expression.
So it doesn’t exist then, does it?
A pity, but that does explain why I couldn’t find anything.
I decided to write a .as_dict() method, because I need the flexibility of a non-default dict constructor.
It works well enough, and perhaps it improves clarity compared with my original idea.
It does exists. You need to convince python that your dataclass is a mapping.
To do that, you need to implement keys and __getitem__:
from dataclasses import fields
class FeedSpecification:
# ...
def keys(self):
return (f.name for f in fields(self))
def __getitem__(self, item):
return getattr(self, item)
You could even write that functionality as a mixin, so it can be re-used for different dataclasses. But IMO using asdict would be clearer to other readers of the code.