Message299553
Maybe the issue is that I work with SQL constantly. In SQL, if I say "SELECT a, b, c FROM t" and table t has columns a, b, c, d, e, f, I can still select a, b, and c from the result. So to me it is natural that getting a bunch of attributes returns something (row or object, depending on the context), where the attributes are still labelled.
I understand why this was rejected as a universal change to attrgetter - in particular, I didn't re-evaluate the appropriateness of the change once I realized that attrgetter has a C implementation - but I don't understand why this isn't considered a natural option to provide.
Using rename=True is just a way of having it not blow up if an attribute name requiring renaming is supplied. I agree that actually using such an attribute requires either guessing the name generated by the rename logic in namedtuple or using numeric indexing. If namedtuple didn't have rename=True then I wouldn't try to re-implement it but since it does I figure it's worth typing ", rename=True" once - it's hardly going to hurt anything.
Finally as to use cases, I agree that if the only thing one is doing is sorting it doesn't matter. But with groupby it can be very useful. Say I have an iterator providing objects with fields (heading_id, heading_text, item_id, item_text). I want to display each heading, followed by its items.
So, I groupby attrgetter ('heading_id', 'heading_text'), and write a loop something like this:
for heading, items in groupby (source, attrgetter ('heading_id', 'heading_text')):
# display heading
# refer to heading.heading_id and heading.heading_text
for item in items:
# display item
# refer to item.item_id and item.item_text
Except I can't, because heading doesn't have attribute names. If I replace attrgetter with namedattrgetter then I'm fine. How would you write this? In the past I've used items[0] but that is (a) ugly and (b) requires "items = list(items)" which is just noise.
I feel like depending on what is being done with map and filter you could have a similar situation where you want to refer to the specific fields of the tuple coming back from the function returned by attrgetter. |
|
Date |
User |
Action |
Args |
2017-07-31 12:39:16 | Isaac Morland | set | recipients:
+ Isaac Morland, rhettinger |
2017-07-31 12:39:16 | Isaac Morland | set | messageid: <1501504756.77.0.554623457588.issue31086@psf.upfronthosting.co.za> |
2017-07-31 12:39:16 | Isaac Morland | link | issue31086 messages |
2017-07-31 12:39:16 | Isaac Morland | create | |
|