Trouble sorting a list of objects by attributes

Terry Reedy tjreedy at udel.edu
Fri Feb 6 17:34:51 EST 2009


Robocop wrote:

> UH OH GUYS!
> 
> line 110, in sorter
>     timesheets.sort(key=attrgetter("department", "engagement",
> "date","start"))
> TypeError: attrgetter expected 1 arguments, got 4

Um... what version of Python are you running?  Alway specify. (Too many 
people do not).  In 3.0

from operator import attrgetter
f=attrgetter("department", "engagement","date","start")

runs fine as per the doc.

operator.attrgetter(attr[, args...])
Return a callable object that fetches attr from its operand. If more 
than one attribute is requested, returns a tuple of attributes. After, f 
= attrgetter('name'), the call f(b) returns b.name. After, f = 
attrgetter('name', 'date'), the call f(b) returns (b.name, b.date).




More information about the Python-list mailing list