Minor fix-ups to named tuples:
* Make the _replace() method respect subclassing.
* Using property() to make _fields read-only wasn't a good idea.
It caused len(Point._fields) to fail.
* Add note to _cast() about length checking and alternative with the star-operator.
diff --git a/Lib/collections.py b/Lib/collections.py
index c6d0d0f..487b119 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -60,6 +60,7 @@
template = '''class %(typename)s(tuple):
'%(typename)s(%(argtxt)s)' \n
__slots__ = () \n
+ _fields = %(field_names)r \n
def __new__(cls, %(argtxt)s):
return tuple.__new__(cls, (%(argtxt)s)) \n
_cast = classmethod(tuple.__new__) \n
@@ -70,10 +71,7 @@
return {%(dicttxt)s} \n
def _replace(self, **kwds):
'Return a new %(typename)s object replacing specified fields with new values'
- return %(typename)s._cast(map(kwds.get, %(field_names)r, self)) \n
- @property
- def _fields(self):
- return %(field_names)r \n\n''' % locals()
+ return self.__class__._cast(map(kwds.get, %(field_names)r, self)) \n\n''' % locals()
for i, name in enumerate(field_names):
template += ' %s = property(itemgetter(%d))\n' % (name, i)
if verbose: