== My current somewhat tangled feelings on _operator.attrgetter_ In a comment on [[my recent entry on sort comparison functions SortCmpFunctionClarityIssue]], [[Peter Donis http://blog.peterdonis.com/]] 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_ https://docs.python.org/2.7/library/operator.html#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.)