Skip to content

Commit 3cfe5b7

Browse files
authored
Simplify creation of the __new__ method in namedtuple() (GH-20361)
1 parent a2bbedc commit 3cfe5b7

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

Lib/collections/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,9 @@ def namedtuple(typename, field_names, *, rename=False, defaults=None, module=Non
406406

407407
# Create all the named tuple methods to be added to the class namespace
408408

409-
s = f'def __new__(_cls, {arg_list}): return _tuple_new(_cls, ({arg_list}))'
409+
s = f'lambda _cls, {arg_list}: _tuple_new(_cls, ({arg_list}))'
410410
namespace = {'_tuple_new': tuple_new, '__name__': f'namedtuple_{typename}'}
411-
# Note: exec() has the side-effect of interning the field names
412-
exec(s, namespace)
413-
__new__ = namespace['__new__']
411+
__new__ = eval(s, namespace)
414412
__new__.__doc__ = f'Create new instance of {typename}({arg_list})'
415413
if defaults is not None:
416414
__new__.__defaults__ = defaults

0 commit comments

Comments
 (0)