4 ms·
I don't like `__post_init__` either. Wouldn't it make more sense to just override `__init__` similar to: @dataclass class DataObj: some_date:
by cpburns2009 9y ago
I don't like `__post_init__` either. Wouldn't it make more sense to just override `__init__` similar to:
@dataclass
class DataObj:
some_date: datetime
def __init__(self, **kw):
kw['some_date'] = dateutil.parser.parse(kw['some_date'])
super().__init__(**kw)
- dragonwriter 9y agoTo allow you to override init that way, @dataclass would have to create an additional superclass with the generated init method, rather than generating the init method in the class intended for use, which seems to have more overhead.
- cpburns2009 9y agoYeah, good point. I didn't think that through very much.
- ptx 9y agoYou could attach the __init__ method to dataclass (as if it were a class) and call it like this, couldn't you? dataclass.__init__(self, **kw) Like in the olden days before super().
- dragonwriter 9y agoFrom the description, the dataclass decorator generates an __init__ method for the decorated class, which seems different than using a common generic __init__ method for all classes with the decorator.
- bobbyi_settv 9y agoIf you want to do it that way, I imagine you could use regular subclassing @dataclass class BaseDataObj: some_date: datetime class DataObj(BaseDataObj): def __init__(self, **kw): kw['some_date'] = dateutil.parser.parse(kw['some_date']) super().__init__(**kw)