Expand Up
@@ -301,7 +301,10 @@ def __eq__(self, other):
### namedtuple
################################################################################
_nt_itemgetters = {}
try:
from _collections import _tuplegetter
except ImportError:
_tuplegetter = lambda index: property(_itemgetter(index))
def namedtuple(typename, field_names, *, rename=False, module=None):
"""Returns a new subclass of tuple with named fields.
Expand Down
Expand Up
@@ -427,15 +430,10 @@ def __getnewargs__(self):
'_asdict': _asdict,
'__getnewargs__': __getnewargs__,
}
cache = _nt_itemgetters
for index, name in enumerate(field_names):
try:
itemgetter_object, doc = cache[index]
except KeyError:
itemgetter_object = _itemgetter(index)
doc = f'Alias for field number {index}'
cache[index] = itemgetter_object, doc
class_namespace[name] = property(itemgetter_object, doc=doc)
tuplegetter = _tuplegetter(index)
tuplegetter.__doc__ = f'Alias for field number {index}'
class_namespace[name] = tuplegetter
result = type(typename, (tuple,), class_namespace)
Expand Down