[New-bugs-announce] [issue31085] Add option for namedtuple to name its result type automatically

Isaac Morland report at bugs.python.org
Sun Jul 30 21:05:54 EDT 2017


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 <report at bugs.python.org>
<http://bugs.python.org/issue31085>
_______________________________________


More information about the New-bugs-announce mailing list