3 ms·
I'm happy to see data classes. I think something like this exists in 3.6: class Person(typing.NamedTuple): name: str age: int But I don't
by weberc2 9y ago
I'm happy to see data classes. I think something like this exists in 3.6:
class Person(typing.NamedTuple):
name: str
age: int
But I don't think it supports the __post_init__; however, constructors have no business doing parsing like this anyway, so unless I'm missing something, deriving from `typing.NamedTuple` seems strictly better than `@dataclass` insofar as it seems less likely to be abused.
- mixmastamyk 9y agoTuples are read-only.
- weberc2 9y agoAh, of course. Good point. I tend to write things in an immutable style, so I don't usually pay attention to this.
- silviogutierrez 9y agoI see immutability as a feature. There are uses cases for mutability, but it should be opt-in, not opt-out. So I'm not loving the fact that "frozen" (the mutability param for data classes) defaults to False.
- weberc2 9y agoI agree, which is part of the reason I think I'd be inclined to derive from NamedTuple and forego data classes.
- icebraining 9y agoPython is very much not immutable by default, it'd be weird to subvert it in a specific case.
- caioariede 9y agoAlso, AFAIK you cannot add methods to NamedTuple, making it much less flexible than data classes.
- kevinmgranger 9y agoOne of the examples in the documentation uses a method. https://docs.python.org/3/library/typing.html#typing.NamedTuple https://docs.python.org/3/library/typing.html#typing.NamedTu...
- caioariede 9y agoYeah, you're right. You just cannot overwrite __init__: > AttributeError: Cannot overwrite NamedTuple attribute __init__