Sorting a list of objects by multiple attributes
Kent Johnson
kent at kentsjohnson.com
Tue Apr 11 12:14:44 EDT 2006
Scott David Daniels wrote:
> Kent Johnson wrote:
>> In Python 2.5 you can do this with operator.attrgetter():
>> L.sort(key=operator.attrgetter('whatever', 'someother', 'anotherkey'))
>
> Note: this is also available in Python 2.4
No, the ability to specify more than one attribute name, making a getter
that returns a tuple, is a Python 2.5 enhancement. In 2.4:
In [1]: import operator
In [2]: operator.attrgetter('whatever', 'someother', 'anotherkey')
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in ?
TypeError: attrgetter expected 1 arguments, got 3
Kent
More information about the Python-list
mailing list