Typed version of namedtuple. Usage:: class Employee(NamedTuple): name: str id: int This is equivalent to:: Employee = collections.namedtuple('Employee', ['name', 'id']) The resulting class has an extra __annotations__ attribute, giving a d
(typename, fields, /)
| 3088 | |
| 3089 | |
| 3090 | def NamedTuple(typename, fields, /): |
| 3091 | """Typed version of namedtuple. |
| 3092 | |
| 3093 | Usage:: |
| 3094 | |
| 3095 | class Employee(NamedTuple): |
| 3096 | name: str |
| 3097 | id: int |
| 3098 | |
| 3099 | This is equivalent to:: |
| 3100 | |
| 3101 | Employee = collections.namedtuple('Employee', ['name', 'id']) |
| 3102 | |
| 3103 | The resulting class has an extra __annotations__ attribute, giving a |
| 3104 | dict that maps field names to types. (The field names are also in |
| 3105 | the _fields attribute, which is part of the namedtuple API.) |
| 3106 | An alternative equivalent functional syntax is also accepted:: |
| 3107 | |
| 3108 | Employee = NamedTuple('Employee', [('name', str), ('id', int)]) |
| 3109 | """ |
| 3110 | types = {n: _type_check(t, f"field {n} annotation must be a type") |
| 3111 | for n, t in fields} |
| 3112 | nt = _make_nmtuple(typename, types, _make_eager_annotate(types), module=_caller()) |
| 3113 | nt.__orig_bases__ = (NamedTuple,) |
| 3114 | return nt |
| 3115 | |
| 3116 | _NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {}) |
| 3117 |
searching dependent graphs…