[Python-checkins] Complete and neaten-up namedtuple's replacement of builtin function lookups with derefs (GH-11794)

Miss Islington (bot) webhook-mailer at python.org
Fri Feb 8 13:37:45 EST 2019


https://github.com/python/cpython/commit/64360ada0f6123a051e9dc6cd04f030ec1322e46
commit: 64360ada0f6123a051e9dc6cd04f030ec1322e46
branch: master
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
date: 2019-02-08T10:37:39-08:00
summary:

Complete and neaten-up namedtuple's replacement of builtin function lookups with derefs (GH-11794)

files:
M Lib/collections/__init__.py

diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index aee79b931602..d88c4aaaee89 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -390,7 +390,7 @@ def namedtuple(typename, field_names, *, rename=False, defaults=None, module=Non
     arg_list = repr(field_names).replace("'", "")[1:-1]
     repr_fmt = '(' + ', '.join(f'{name}=%r' for name in field_names) + ')'
     tuple_new = tuple.__new__
-    _len = len
+    _dict, _tuple, _len, _map, _zip = dict, tuple, len, map, zip
 
     # Create all the named tuple methods to be added to the class namespace
 
@@ -414,7 +414,7 @@ def _make(cls, iterable):
                               'or iterable')
 
     def _replace(_self, **kwds):
-        result = _self._make(map(kwds.pop, field_names, _self))
+        result = _self._make(_map(kwds.pop, field_names, _self))
         if kwds:
             raise ValueError(f'Got unexpected field names: {list(kwds)!r}')
         return result
@@ -426,18 +426,15 @@ def __repr__(self):
         'Return a nicely formatted representation string'
         return self.__class__.__name__ + repr_fmt % self
 
-    _dict, _zip = dict, zip
-
     def _asdict(self):
         'Return a new dict which maps field names to their values.'
         return _dict(_zip(self._fields, self))
 
     def __getnewargs__(self):
         'Return self as a plain tuple.  Used by copy and pickle.'
-        return tuple(self)
+        return _tuple(self)
 
     # Modify function metadata to help with introspection and debugging
-
     for method in (__new__, _make.__func__, _replace,
                    __repr__, _asdict, __getnewargs__):
         method.__qualname__ = f'{typename}.{method.__name__}'



More information about the Python-checkins mailing list