attrgetter intended for use in places where a function to replace lambda is required. For instance:
# equivalent heads = map(attrgetter('head'), objs) heads = map(lambda o: o.head, objs)
In other words, the point of an attrgetter is to create a function without side effects that returns a useful value and that can be used in expressions that require a function. On the other hand, an attrsetter will only act on a side effect and should return None by Python convention. Since an attrsetter would not be generally useful as an argument to map and the like, it is not provided. If you need an attrsetter , just write a regular for loop.
Also note that both of the above idioms are better expressed with list comprehension:
heads = [o.head for o in objs]
attrgetter rarely required and has lost most of its appeal once it was announced that lambda would not be removed from Python 3 at the end.
source share