And what about recursive structures? I do think this is a rabbit hole worth getting into as I've written an iffy dict serializer (and differ) for an audit system at work. There are many uses for such a thing I think. But maybe we just need some informal protocol?
> On 3 May 2019, at 19:00, Rhodri James <rho...@kynesim.co.uk> wrote: > >> On 03/05/2019 17:37, Ricky Teachey wrote: >> I just joined the ideas list today so I do not know if this has been >> discussed. >> Using dataclasses has been great for me, but a challenge is what to do when >> you don't *know* if the object you are using is a dataclass, and could be a >> variety of other classes. >> This could easily occur when you don't want to assume the data type being >> used by the user to represent their data, but wish to turn it into a dict >> (if an API exists to do so). > > Conventionally you would get the caller to do this, because only the caller > knows what format they have actually got. Anything else involves lots of > guesswork and, as you point out, gets unweildy and silly very quickly. > >> The idea is: 1. identifying the various "asdict" APIs used in the standard >> library, and 2. include a keyword option for dataclasses.asdict() to cast a >> non-dataclasses object to a dict using these protocols (and falling back on >> factory_dict(obj) if it is provided). > > Are "asdict" APIs all that common? I can't recall feeling much need for > them, but I'm hardly a good test case :-) > > The problem with "casting" a generic non-dataclass object to a dict is that > it's not remotely obvious how you do that. What key do you use if your > object is a simple int or string? If you have even a straightforward user > class aren't just using "thing.__dict__", what attributes do or do not make > it into the dictionary? > > I'm sorry, but I'm not convinced that this is a rabbit-hole that it will be > useful to dive down. > > -- > Rhodri James *-* Kynesim Ltd > _______________________________________________ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/