This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eric.smith
Recipients eric.smith, gsakkis, matrixise, rhettinger, xtreak
Date 2019-04-19.08:55:08
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1555664108.69.0.433044301212.issue36662@roundup.psfhosted.org>
In-reply-to
Content
I think the best thing to do is write another decorator that adds this method. I've often thought that having a dataclasses_tools third-party module would be a good idea. It could include my add_slots decorator in https://github.com/ericvsmith/dataclasses/blob/master/dataclass_tools.py

Such a decorator could then deal with all the complications that I don't want to add to @dataclass. For example, choosing a method name. @dataclass doesn't inject any non-dunder names in the class, but the new decorator could, or it could provide a way to customize the member name.

Also, note that your example asdict method doesn't do the same thing as dataclasses.asdict. While you get some speedup by knowing the field names in advance, you also don't do the recursive generation that dataclasses.asdict does. In order to skip the recursive dict generation, you'd either have to test the type of each member (using some heuristic about what doesn't need recursion), or assume the member type matches the type defined in the class. I don't want dataclasses.asdict to make the assumption that the member type matches the declared type. There's nowhere else it does this.

I'm not sure how much of the speedup you're seeing is the result of hard-coding the member names, and how much is avoiding recursion. If all of the improvement is by eliminating recursion, then it's not worth doing.

I'm not saying the existing dataclasses.asdict can't be sped up: surely it can. But I don't want to remove features or add complexity to do so.
History
Date User Action Args
2019-04-19 08:55:08eric.smithsetrecipients: + eric.smith, rhettinger, gsakkis, matrixise, xtreak
2019-04-19 08:55:08eric.smithsetmessageid: <1555664108.69.0.433044301212.issue36662@roundup.psfhosted.org>
2019-04-19 08:55:08eric.smithlinkissue36662 messages
2019-04-19 08:55:08eric.smithcreate