diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2020-06-08 19:38:41 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-08 19:38:41 (GMT) |
commit | 0a40849eb99a0357113bff10434ec6605e3ae96b (patch) | |
tree | 28caeb8d192123cb6a109f8a518b48dbbdf779a5 /Lib/collections | |
parent | 3ab3475c42c8ee5580f4ea1aeda73ebc8e5d5478 (diff) | |
download | cpython-0a40849eb99a0357113bff10434ec6605e3ae96b.zip cpython-0a40849eb99a0357113bff10434ec6605e3ae96b.tar.gz cpython-0a40849eb99a0357113bff10434ec6605e3ae96b.tar.bz2 |
Minor improvement to the namedtuple implementation (GH-20741)
* Cleaner way to build the arg list with a trailing comma when required
* Fix appearance of __new__ in help()
Diffstat (limited to 'Lib/collections')
-rw-r--r-- | Lib/collections/__init__.py | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index 1e3b54c..6a06cc6 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -399,7 +399,9 @@ def namedtuple(typename, field_names, *, rename=False, defaults=None, module=Non # Variables used in the methods and docstrings field_names = tuple(map(_sys.intern, field_names)) num_fields = len(field_names) - arg_list = repr(field_names).replace("'", "")[1:-1] + arg_list = ', '.join(field_names) + if num_fields == 1: + arg_list += ',' repr_fmt = '(' + ', '.join(f'{name}=%r' for name in field_names) + ')' tuple_new = tuple.__new__ _dict, _tuple, _len, _map, _zip = dict, tuple, len, map, zip @@ -410,6 +412,7 @@ def namedtuple(typename, field_names, *, rename=False, defaults=None, module=Non namespace = {'_tuple_new': tuple_new, '__builtins__': None, '__name__': f'namedtuple_{typename}'} __new__ = eval(s, namespace) + __new__.__name__ = '__new__' __new__.__doc__ = f'Create new instance of {typename}({arg_list})' if defaults is not None: __new__.__defaults__ = defaults |