In Python, I often find myself needing an something like NamedTuple (immutable, typed), but with an overrideable constructor and extendability.
The (Python 3.6+) Syntax
class MyThing(NamedTuple):
thing1: int
thing2: float
Is nice, but it:
- Doesn't let you override the
__new__
constructor, which is useful for parameter checking and type coercion. Cannot be extended in a logical way: i.e.
class MySpecificThing(MyThing): thing3: str
Cannot be constructed with
obj = MySpecificThing(thing1=2, thing2=2.5, thing3='aaa')
(You get
TypeError: __new__() got an unexpected keyword argument 'thing3'
)
Does anybody know of a data structure that has all of these nice properties (immutability, typed fields, extendability, and customizable construction)?