[issue46757] dataclasses should define an empty __post_init__

Neil Girdhar report at bugs.python.org
Sat Feb 19 12:13:14 EST 2022


Neil Girdhar <mistersheik at gmail.com> added the comment:

> I'm not crazy about adding a method to every dataclass just for the 0.1% of the times it's needed.

I'm not sure I totally understand the cost here.  You can have a single shared global function that you set on each dataclass.  So the only cost would be an entry in each dataclass type's dict.  Even if a user creates a thousand dataclasses, that should only be tens of killobytes in pointers.

> I think using hasattr or catching the exception is a better way to go.

If you choose this, then I think this should be documented under the __post_init__ saying that any time you define __post_init__, you should either be a final class, or else call super.  If you call super, you musteither use hasattr(super().__post_init__) or catch the exception.

I have to admit, I find this quite ugly from a user perspective.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46757>
_______________________________________


More information about the Python-bugs-list mailing list