diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2020-05-26 04:39:00 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-26 04:39:00 (GMT) |
commit | 3cfe5b7b8fb3a0396e62800f3873d9b1f70da1c2 (patch) | |
tree | d68d5ec08b4d74ad846a5261a1523f65180249af /Lib/collections | |
parent | a2bbedc8b18c001d2f9e702e6e678efbb2990daa (diff) | |
download | cpython-3cfe5b7b8fb3a0396e62800f3873d9b1f70da1c2.zip cpython-3cfe5b7b8fb3a0396e62800f3873d9b1f70da1c2.tar.gz cpython-3cfe5b7b8fb3a0396e62800f3873d9b1f70da1c2.tar.bz2 |
Simplify creation of the __new__ method in namedtuple() (GH-20361)
Diffstat (limited to 'Lib/collections')
-rw-r--r-- | Lib/collections/__init__.py | 6 |
1 files changed, 2 insertions, 4 deletions
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index c4bff59..011a0c1 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -406,11 +406,9 @@ def namedtuple(typename, field_names, *, rename=False, defaults=None, module=Non # Create all the named tuple methods to be added to the class namespace - s = f'def __new__(_cls, {arg_list}): return _tuple_new(_cls, ({arg_list}))' + s = f'lambda _cls, {arg_list}: _tuple_new(_cls, ({arg_list}))' namespace = {'_tuple_new': tuple_new, '__name__': f'namedtuple_{typename}'} - # Note: exec() has the side-effect of interning the field names - exec(s, namespace) - __new__ = namespace['__new__'] + __new__ = eval(s, namespace) __new__.__doc__ = f'Create new instance of {typename}({arg_list})' if defaults is not None: __new__.__defaults__ = defaults |