2014-10-28
My current somewhat tangled feelings on operator.attrgetter
In a comment on my recent entry on sort comparison functions, Peter Donis asked a good question:
Is there a reason you're not using operator.attrgetter for the key functions? It's faster than a lambda.
One answer is that until now I hadn't heard of operator.attrgetter
.
Now that I have it's something I'll probably consider in the future.
But another answer is embedded in the reason Peter Donis gave for
using it. Using operator.attrgetter
is clearly a speed optimization,
but speed isn't always the important thing. Sometimes, even often,
the most important thing to optimize is clarity. Right now, for me
attrgetter is less clear than the lambda
approach because I've
just learned about it; switching to it would probably be a premature
optimization for speed at the cost of clarity.
In general, well, 'attrgetter' is a clear enough thing
that I suspect I'll never be confused about what
'lst.sort(key=operator.attrgetter("field"))
' does, even if I forget
about it and then reread some code that uses it; it's just pretty
obvious from context and the name itself. There's a visceral bit of
me that doesn't like it as much as the lambda
approach because I
don't think it reads as well, though. It's also more black magic than
lambda
, since lambda
is a general language construct and attrgetter
is a magic module function.
(And as a petty thing it has less natural white space. I like white space since it makes things more readable.)
On the whole this doesn't leave me inclined to switch to using
attrgetter for anything except performance sensitive code (which these
sort()
s aren't so far). Maybe this is the wrong decision, and if the
Python community as a whole adopts attrgetter as the standard and usual
way to do .sort()
key access it certainly will become a wrong decision.
At that point I hope I'll notice and switch myself.
(This is an sense an uncomfortable legacy of CPython's historical
performance issues with Python code. Attrgetter is clearly a performance
hack in general; if lambda
was just as fast as it I'd argue that you
should clearly use lambda
because it's a general language feature
instead of a narrowly specialized one.)