summaryrefslogtreecommitdiffstats
path: root/Lib/collections
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-03-23 05:36:21 (GMT)
committerRaymond Hettinger <python@rcn.com>2011-03-23 05:36:21 (GMT)
commitb2d0945c8783ab315520169c4bd4533ad82ad539 (patch)
tree4e78926247918273bb1a7bb9daec0ab3c933b9ce /Lib/collections
parentfef854602bb86e396a1db82f91e9321eb16f4a97 (diff)
downloadcpython-b2d0945c8783ab315520169c4bd4533ad82ad539.zip
cpython-b2d0945c8783ab315520169c4bd4533ad82ad539.tar.gz
cpython-b2d0945c8783ab315520169c4bd4533ad82ad539.tar.bz2
Minor named tuple clean-ups.
Diffstat (limited to 'Lib/collections')
-rw-r--r--Lib/collections/__init__.py19
1 files changed, 9 insertions, 10 deletions
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index b429599..d4e8ed6 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -265,7 +265,7 @@ class {typename}(tuple):
'Return a new {typename} object replacing specified fields with new values'
result = _self._make(map(kwds.pop, {field_names!r}, _self))
if kwds:
- raise ValueError('Got unexpected field names: %r' % kwds.keys())
+ raise ValueError('Got unexpected field names: %r' % list(kwds))
return result
def __getnewargs__(self):
@@ -309,18 +309,17 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
# generating informative error messages and preventing template injection attacks.
if isinstance(field_names, str):
field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas
- field_names = tuple(map(str, field_names))
+ field_names = list(map(str, field_names))
if rename:
- names = list(field_names)
seen = set()
- for i, name in enumerate(names):
- if (not all(c.isalnum() or c=='_' for c in name) or _iskeyword(name)
+ for index, name in enumerate(field_names):
+ if (not all(c.isalnum() or c=='_' for c in name)
+ or _iskeyword(name)
or not name or name[0].isdigit() or name.startswith('_')
or name in seen):
- names[i] = '_%d' % i
+ field_names[index] = '_%d' % index
seen.add(name)
- field_names = tuple(names)
- for name in (typename,) + field_names:
+ for name in [typename] + field_names:
if not all(c.isalnum() or c=='_' for c in name):
raise ValueError('Type names and field names can only contain alphanumeric characters and underscores: %r' % name)
if _iskeyword(name):
@@ -338,9 +337,9 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
# Fill-in the class template
class_definition = _class_template.format(
typename = typename,
- field_names = field_names,
+ field_names = tuple(field_names),
num_fields = len(field_names),
- arg_list = repr(field_names).replace("'", "")[1:-1],
+ arg_list = repr(tuple(field_names)).replace("'", "")[1:-1],
repr_fmt = ', '.join(_repr_template.format(name=name) for name in field_names),
field_defs = '\n'.join(_field_template.format(index=index, name=name)
for index, name in enumerate(field_names))