typing.NamedTuple
https://docs.python.org/ja/3/library/typing.html#typing.NamedTuple
collections.namedtuple() の型付き版です。
collections.namedtupleでは「class キーワードを使った洗練された記法」とのこと
code:python
>> from typing import NamedTuple
>> class Employee(NamedTuple):
... name: str
... id: int
>> e1 = Employee("taro", 101)
>> e1
Employee(name='taro', id=101)
>> e1.id
101
>> e1.id = 102
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
tupleなのでobject.__setattr__でも変更できない
code:python
>> object.__setattr__(e1, "id", 102)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
しかしtupleなのでunpackできてしまう
PEP 557 – Data Classesでも挙がっている話
code:python
>> a, b = e1
>> a
'taro'
>> b
101
dataclass(frozen=True)とどっちもどっちという感じ(どちらも図抜けていない)