[Python-ideas] add an additional dataclasses.asdict option for non-dataclasses

Ricky Teachey ricky at teachey.org
Fri May 10 22:27:55 EDT 2019


>
> Sure — but that is inherent in this whole idea — nothing other than the
> type object itself could possibly know how to dictify itself.
>

This seems to have an obvious solution: object.__as_mapping__() could
simply call dict(self). Anything that uses the mapping protocol could then
be mappified.

Though yeah, that’s the key question— how often is this useful???
>

I'll try to make a quick case it is.

Dataclasses have existed for barely a year. I don't know how widely used
they are yet, by I sure use the beschtick out of them. The API is very nice
and I fully expect them to be wildly successful.

But the dclses API encourages the use of the asdict function rather than
the mapping protocol-- if they become successful and widely used, it will
become more and more inconvenient to have to test for multiple mapping APIs.

And this isn't the first alt mapping API.

NT is already incredibly successful (to the degree it has been an intention
of the core devs to use it more and more in core python libraries), and as
such there is a ton of if isinstance(obj,NT) obj._asdict() else dict(obj)
code out there.

Another dictish type in the std lib is types.SimpleNamespace, which is
typically dictified (or rather, re-dictified) via yet another API,
dict(obj.__dict__).

The more we pile these APIs upon APIs, the more ridiculous it gets to
create a flexible API of your own that allows the user to choose what kind
of structure representation THEY want to provide.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190510/c66cf5c1/attachment.html>


More information about the Python-ideas mailing list