Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
0d5048cb
Commit
0d5048cb
authored
Sep 12, 2016
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #17941: Add a *module* parameter to collections.namedtuple()
parent
11fa3ffc
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
22 additions
and
7 deletions
+22
-7
Doc/library/collections.rst
Doc/library/collections.rst
+6
-1
Lib/collections/__init__.py
Lib/collections/__init__.py
+10
-6
Lib/test/test_collections.py
Lib/test/test_collections.py
+4
-0
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Doc/library/collections.rst
View file @
0d5048cb
...
...
@@ -763,7 +763,7 @@ Named tuples assign meaning to each position in a tuple and allow for more reada
self-documenting code. They can be used wherever regular tuples are used, and
they add the ability to access fields by name instead of position index.
.. function:: namedtuple(typename, field_names, *, verbose=False, rename=False)
.. function:: namedtuple(typename, field_names, *, verbose=False, rename=False
, module=None
)
Returns a new tuple subclass named *typename*. The new subclass is used to
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.
built. This option is outdated; instead, it is simpler to print the
:attr:`_source` attribute.
If *module* is defined, the ``__module__`` attribute of the named tuple is
set to that value.
Named tuple instances do not have per-instance dictionaries, so they are
lightweight and require no more memory than regular tuples.
...
...
@@ -800,6 +803,8 @@ they add the ability to access fields by name instead of position index.
The *verbose* and *rename* parameters became
:ref:`keyword-only arguments <keyword-only_parameter>`.
.. versionchanged:: 3.6
Added the *module* parameter.
.. doctest::
:options: +NORMALIZE_WHITESPACE
...
...
Lib/collections/__init__.py
View file @
0d5048cb
...
...
@@ -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
...
...
Lib/test/test_collections.py
View file @
0d5048cb
...
...
@@ -242,6 +242,10 @@ class TestNamedTuple(unittest.TestCase):
]:
self
.
assertEqual
(
namedtuple
(
'NT'
,
spec
,
rename
=
True
).
_fields
,
renamed
)
def
test_module_parameter
(
self
):
NT
=
namedtuple
(
'NT'
,
[
'x'
,
'y'
],
module
=
collections
)
self
.
assertEqual
(
NT
.
__module__
,
collections
)
def
test_instance
(
self
):
Point
=
namedtuple
(
'Point'
,
'x y'
)
p
=
Point
(
11
,
22
)
...
...
Misc/NEWS
View file @
0d5048cb
...
...
@@ -159,6 +159,8 @@ Library
-
Issue
#
10740
:
sqlite3
no
longer
implicitly
commit
an
open
transaction
before
DDL
statements
.
-
Issue
#
17941
:
Add
a
*
module
*
parameter
to
collections
.
namedtuple
().
-
Issue
#
22493
:
Inline
flags
now
should
be
used
only
at
the
start
of
the
regular
expression
.
Deprecation
warning
is
emitted
if
uses
them
in
the
middle
of
the
regular
expression
.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment