I\'ve got a list of Python objects that I\'d like to sort by an attribute of the objects themselves. The list looks like:
>>> ut
[,
If the attribute you want to sort by is a property, then you can avoid importing operator.attrgetter
and use the property's fget method instead.
For example, for a class Circle
with a property radius
we could sort a list of circles
by radii as follows:
result = sorted(circles, key=Circle.radius.fget)
This is not the most well-known feature but often saves me a line with the import.