classification
Title: namedtuple should not hardcode the class name in __repr__
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: PAG, rhettinger
Priority: normal Keywords: patch

Created on 2010-08-04 11:48 by PAG, last changed 2010-08-08 01:15 by rhettinger. This issue is now closed.

Files
File name Uploaded Description Edit
collections-namedtuple-repr.diff PAG, 2010-08-04 11:48 Patch collections.namedtuple, add a testcase to test_collections.py, and update the docs in collections.rst
Messages (2)
msg112792 - (view) Author: Paul Giannaros (PAG) Date: 2010-08-04 11:48
collections.namedtuple hardcodes the class name which is reported in the new type's __repr__. This is irritating when subclassing a namedtuple:

A = collections.namedtuple('A', '')
class B(A):
    pass
print B()  # shows 'A()'

It might not be often that they're subclassed, but it can be a useful way to add extra methods, properties, and documentation. Other classes often use the current instance's class name in the repr (e.g. collections.OrderedDict). The attached patch changes namedtuple to do this, includes a testcase, and updates the documentation.
msg113225 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-08-08 01:15
Applied in r83808 for Python 3.2.

Am not backporting because I don't consider it a bug
and the documented subclassing technique is:
   class A(namedtuple('A', 'x y')):
       ...
History
Date User Action Args
2010-08-08 01:15:11rhettingersetstatus: open -> closed
versions: - Python 2.6, Python 3.1, Python 2.7
messages: + msg113225

resolution: accepted
stage: patch review -> resolved
2010-08-04 15:00:52rhettingersetassignee: rhettinger
2010-08-04 14:51:09r.david.murraysetstage: patch review
versions: + Python 3.1, Python 3.2, - Python 3.3
2010-08-04 14:48:32r.david.murraysetnosy: + rhettinger
2010-08-04 11:48:13PAGcreate