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

Ricky Teachey ricky at teachey.org
Sat May 11 20:13:09 EDT 2019


Using iter() for the sole purpose of being able to shoehorn your user class
into being dict-cast-able is, IMO, an abuse of iter.

If your class makes logical sense as an iterable of key/value pairs, then
for petesake, just use the mapping protocol (implement getitem and keys).
iter() is for constructing sequences, not mappings.

And if the class DOESN'T make logical sense as an interable of key/value
pairs, it still isn't time to abuse iter() for the convenience of making
dicts. Just make an asdict() method.

The above, I believe, is great API advice that I didn't come up with
myself-- and it's so great that it has been followed by both NT and dclses.
But the downside is, we now have the inconvenience of multiple dict-making
APIs.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190511/a0d08c00/attachment.html>


More information about the Python-ideas mailing list