Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0d5048c

Browse files
committed
Issue #17941: Add a *module* parameter to collections.namedtuple()
1 parent 11fa3ff commit 0d5048c

4 files changed

Lines changed: 22 additions & 7 deletions

File tree

Doc/library/collections.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ Named tuples assign meaning to each position in a tuple and allow for more reada
763763
self-documenting code. They can be used wherever regular tuples are used, and
764764
they add the ability to access fields by name instead of position index.
765765

766-
.. function:: namedtuple(typename, field_names, *, verbose=False, rename=False)
766+
.. function:: namedtuple(typename, field_names, *, verbose=False, rename=False, module=None)
767767

768768
Returns a new tuple subclass named *typename*. The new subclass is used to
769769
create tuple-like objects that have fields accessible by attribute lookup as
@@ -790,6 +790,9 @@ they add the ability to access fields by name instead of position index.
790790
built. This option is outdated; instead, it is simpler to print the
791791
:attr:`_source` attribute.
792792

793+
If *module* is defined, the ``__module__`` attribute of the named tuple is
794+
set to that value.
795+
793796
Named tuple instances do not have per-instance dictionaries, so they are
794797
lightweight and require no more memory than regular tuples.
795798

@@ -800,6 +803,8 @@ they add the ability to access fields by name instead of position index.
800803
The *verbose* and *rename* parameters became
801804
:ref:`keyword-only arguments <keyword-only_parameter>`.
802805

806+
.. versionchanged:: 3.6
807+
Added the *module* parameter.
803808

804809
.. doctest::
805810
:options: +NORMALIZE_WHITESPACE

Lib/collections/__init__.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ def __getnewargs__(self):
353353
{name} = _property(_itemgetter({index:d}), doc='Alias for field number {index:d}')
354354
'''
355355

356-
def namedtuple(typename, field_names, *, verbose=False, rename=False):
356+
def namedtuple(typename, field_names, *, verbose=False, rename=False, module=None):
357357
"""Returns a new subclass of tuple with named fields.
358358
359359
>>> Point = namedtuple('Point', ['x', 'y'])
@@ -434,11 +434,15 @@ def namedtuple(typename, field_names, *, verbose=False, rename=False):
434434
# For pickling to work, the __module__ variable needs to be set to the frame
435435
# where the named tuple is created. Bypass this step in environments where
436436
# sys._getframe is not defined (Jython for example) or sys._getframe is not
437-
# defined for arguments greater than 0 (IronPython).
438-
try:
439-
result.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__')
440-
except (AttributeError, ValueError):
441-
pass
437+
# defined for arguments greater than 0 (IronPython), or where the user has
438+
# specified a particular module.
439+
if module is None:
440+
try:
441+
module = _sys._getframe(1).f_globals.get('__name__', '__main__')
442+
except (AttributeError, ValueError):
443+
pass
444+
if module is not None:
445+
result.__module__ = module
442446

443447
return result
444448

Lib/test/test_collections.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ def test_name_fixer(self):
242242
]:
243243
self.assertEqual(namedtuple('NT', spec, rename=True)._fields, renamed)
244244

245+
def test_module_parameter(self):
246+
NT = namedtuple('NT', ['x', 'y'], module=collections)
247+
self.assertEqual(NT.__module__, collections)
248+
245249
def test_instance(self):
246250
Point = namedtuple('Point', 'x y')
247251
p = Point(11, 22)

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ Library
159159
- Issue #10740: sqlite3 no longer implicitly commit an open transaction
160160
before DDL statements.
161161

162+
- Issue #17941: Add a *module* parameter to collections.namedtuple().
163+
162164
- Issue #22493: Inline flags now should be used only at the start of the
163165
regular expression. Deprecation warning is emitted if uses them in the
164166
middle of the regular expression.

0 commit comments

Comments
 (0)