[Python-ideas] add an additional dataclasses.asdict option for non-dataclasses
Brendan Barnwell
brenbarn at brenbarn.net
Sat May 11 15:59:48 EDT 2019
On 2019-05-11 12:50, Christopher Barker wrote:
> On Sat, May 11, 2019 at 4:06 AM Jeff
>
> > I'd much rather have some kind of explicit wrapping or cast into
> their Python type: I had imagined the wrapped object would identify and
> behave as exactly that Python type (or ABC), however, it would be a
> *proxy* to the original.
>
> The ABCs are already defined — if you want to make a wrapper proxy
> object, you implement the ABC you want to emulate.
>
>
> Now that I’m thinking about it more, this really covers most use cases:
> if you want your object to be converted to a dict by passing it to
> dict(), then implement the iteration protocol, returning key-value pairs
> that you want. This would work fine for dataclasses, and
> SimpleNamespaces, if one wanted to implement that.
>
> The only problem here is if you want to use the standard iteration
> protocol for something different -- I can't image what that would be.
> Sure, on the general case, maybe, but for a class that has a "natural"
> use case for dictifying, how else would you want to iterate over it??
One example would be. . . dicts. Calling dict on a dict gives you a
dict, but iterating over the dict just gives you the keys. Certain
other mapping types (such as Pandas Series and DataFrames) also have
this behavior where iterating over the mapping gives you only the keys.
--
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is no
path, and leave a trail."
--author unknown
More information about the Python-ideas
mailing list