operator.attrgetter()
operator.attrgetter() is a Python function in the Operator Module category. Returns a callable that fetches the specified attribute(s) from its operand. The syntax is operator.attrgetter(*attrs). Returns: Callable getter. A typical example: from operator import attrgetter
users = [User("Alice"), User("Bob")]
sorted(users, key=attrgetter("name")). A close sibling is operator.add(), which returns a + b (addition). Useful with reduce and map. A close sibling is operator.sub(), which returns a - b (subtraction). A close sibling is operator.mul(), which returns a * b (multiplication). A close sibling is operator.itemgetter(), which returns a callable that fetches the specified item(s) from its operand. More about this category: Operator functions — add, mul, itemgetter, attrgetter, methodcaller. Related Operator Module entries: operator.add(), operator.sub(), operator.mul(), operator.itemgetter(), operator.methodcaller(). This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details. You can try operator.attrgetter() immediately in the Python playground, look up unfamiliar terms in the glossary, or return to the Python Reference homepage.