> > 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.
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/