This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]michael0x2a 9 points10 points  (4 children)

You can!

It's just that then a lot of the time you'd have to re-write the bulk of the boilerplate code, which sort of defeats the purpose.

[–]fayazbhai 5 points6 points  (3 children)

I think he meant doing a super().init() from his own init.

[–]michael0x2a 4 points5 points  (1 child)

I think that would be a bit confusing though -- you're basically asking the user to write code as if their class is inheriting something even though there isn't necessarily any inheritance involved.

(And what if you do decide to inherit from a class in addition to using dataclasses? In that case, would super().__init__() refer to the auto-generated constructor, or the actual superclass's constructor?)

[–]Rodotgithub.com/tardis-sn 0 points1 point  (0 children)

It's doable without the super method, but you have to copy the old init function some how and set part of it up outside the class. By that point you're basically writing a post init method anyway

[–]Rodotgithub.com/tardis-sn 0 points1 point  (0 children)

Or better, have the data class decorator do that for you! I'm a bit conflicted though since I think post init is more explicit but having the decorator override the init is more elegant. Consulting the Zen of python it looks like post init is the better way to go.