[issue31085] Add option for namedtuple to name its result type automatically
Isaac Morland
report at bugs.python.org
Sun Jul 30 21:10:38 EDT 2017
Isaac Morland added the comment:
I'm hoping to make a pull request but while I figure that out here is the diff:
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index 8408255..62cf708 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -384,7 +384,6 @@ def namedtuple(typename, field_names, *, verbose=False, rename=False, module=Non
if isinstance(field_names, str):
field_names = field_names.replace(',', ' ').split()
field_names = list(map(str, field_names))
- typename = str(typename)
if rename:
seen = set()
for index, name in enumerate(field_names):
@@ -394,6 +393,10 @@ def namedtuple(typename, field_names, *, verbose=False, rename=False, module=Non
or name in seen):
field_names[index] = '_%d' % index
seen.add(name)
+ if typename is None:
+ typename = '__'.join (field_names)
+ else:
+ typename = str(typename)
for name in [typename] + field_names:
if type(name) is not str:
raise TypeError('Type names and field names must be strings')
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue31085>
_______________________________________
More information about the Python-bugs-list
mailing list