New submission from Isaac Morland:
I would like to have the possibility of creating a namedtuple type without
explicitly giving it a name. I see two major use cases for this:
1) Automatic creation of namedtuples for things like CSV files with headers
(see #1818) or SQL results (see #13299). In this case at the point of calling
namedtuple I have column headings (or otherwise automatically-determined
attribute names), but there probably isn't a specific class name that makes
sense to use.
2) Subclassing from a namedtuple invocation; I obviously need to name my
subclass, but the name passed to the namedtuple invocation is essentially
useless.
My idea is to allow giving None for the typename parameter of namedtuple, like
this:
class MyCustomBehaviourNamedtuple (namedtuple (None, ['a', 'b'])):
...
In this case namedtuple will generate a name based on the field names.
This should be backward compatible because right now passing None raises a
TypeError. So there is no change if a non-None typename is passed, and an
exception is replaced by computing a default typename if None is passed.
Patch to follow.
----------
components: Library (Lib)
messages: 299532
nosy: Isaac Morland
priority: normal
severity: normal
status: open
title: Add option for namedtuple to name its result type automatically
type: enhancement
versions: Python 3.7
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue31085>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com