summaryrefslogtreecommitdiff
path: root/Lib/collections
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2016-09-12 00:18:31 -0700
committerRaymond Hettinger <python@rcn.com>2016-09-12 00:18:31 -0700
commit0d5048cb21e431c1a8221e15563837090946be81 (patch)
treec9e5960273276eb9377a54b8438dac48508161db /Lib/collections
parent11fa3ffcb11caf628435670b213f9bc2e3ba4f67 (diff)
downloadcpython-git-0d5048cb21e431c1a8221e15563837090946be81.tar.gz
Issue #17941: Add a *module* parameter to collections.namedtuple()
Diffstat (limited to 'Lib/collections')
-rw-r--r--Lib/collections/__init__.py16
1 files changed, 10 insertions, 6 deletions
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index 03ecea27cc..bcc429195d 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -353,7 +353,7 @@ _field_template = '''\
{name} = _property(_itemgetter({index:d}), doc='Alias for field number {index:d}')
'''
-def namedtuple(typename, field_names, *, verbose=False, rename=False):
+def namedtuple(typename, field_names, *, verbose=False, rename=False, module=None):
"""Returns a new subclass of tuple with named fields.
>>> Point = namedtuple('Point', ['x', 'y'])
@@ -434,11 +434,15 @@ def namedtuple(typename, field_names, *, verbose=False, rename=False):
# For pickling to work, the __module__ variable needs to be set to the frame
# where the named tuple is created. Bypass this step in environments where
# sys._getframe is not defined (Jython for example) or sys._getframe is not
- # defined for arguments greater than 0 (IronPython).
- try:
- result.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__')
- except (AttributeError, ValueError):
- pass
+ # defined for arguments greater than 0 (IronPython), or where the user has
+ # specified a particular module.
+ if module is None:
+ try:
+ module = _sys._getframe(1).f_globals.get('__name__', '__main__')
+ except (AttributeError, ValueError):
+ pass
+ if module is not None:
+ result.__module__ = module
return result