This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients Isaac Morland, rhettinger
Date 2017-08-01.06:01:13
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1501567275.1.0.291926906484.issue31086@psf.upfronthosting.co.za>
In-reply-to
Content
Sorry Issac, but I'm going to decline this feature request.  I know you're enthusiastic about this or some other variation but I don't think it is worthy of becoming part of the standard library.  I do encourage you to post this somewhere as recipe (personally, I've used the ASPN cookbook to post my ideas) or as an offering on PyPI.

Reasons:

* The use cases are thin and likely to be uncommon.

* The recipe is short and doesn't add much value.

* The anonymous or autogenerated typename is unhelpful
  and the output doesn't look nice.

* It is already possible to combine a namedtuple with field
  extraction using a simple lambda.

* List comprehensions are clearer, easier, and more flexible
  for the task of extracting fields into a new named tuple.

* The combination of an anonymous or autogenerated typename
  along with automatic field renaming will likely cause
  more problems than it is worth.

* I don't expect this to mesh well with typing.NamedTuple
  and the needs of static typing tools

* Debugging may become more challenging with implicitly
  created named tuples that have autogenerated type names.


-- My experiments with the API ------------------------------

from collections import namedtuple
from operator import attrgetter
from pprint import pprint

def namedattrgetter (attr, *attrs):
    ag = attrgetter (attr, *attrs)
    if attrs:
        nt = namedtuple ('_', (attr,) + attrs, rename=True)
        return lambda obj: nt._make (ag (obj))
    else:
        return ag

Person = namedtuple('Person', ['fname', 'lname', 'age', 'email'])
FullName = namedtuple('FullName', ['lname', 'fname'])

people = [
	Person('tom', 'smith', 50, 'tsmith@example.com'),
	Person('sue', 'henry', 40, 'shenry@example.com'),
	Person('hank', 'jones', 30, 'hjones@example.com'),
	Person('meg', 'davis', 20, 'mdavis@example.com'),
]

# Proposed way
pprint(list(map(namedattrgetter('lname', 'fname'), people)))

# Existing way with two-steps (attrgetter followed by nt._make)
pprint(list(map(FullName._make, map(attrgetter(*FullName._fields), people))))

# Existing way using a lambda
pprint(list(map(lambda p: FullName(p.lname, p.fname), people)))

# Best way with a plain list comprehension
pprint([FullName(p.lname, p.fname) for p in people])
History
Date User Action Args
2017-08-01 06:01:15rhettingersetrecipients: + rhettinger, Isaac Morland
2017-08-01 06:01:15rhettingersetmessageid: <1501567275.1.0.291926906484.issue31086@psf.upfronthosting.co.za>
2017-08-01 06:01:15rhettingerlinkissue31086 messages
2017-08-01 06:01:13rhettingercreate