On this page
operator.attrgetter
Prev: operator.and_ | Up: operator | Next: operator.call
Rel: operator.abs, operator.add, operator.and_, operator.call
Ref: docs.python.org — operator.attrgetter
Tags: #public #python3 #stdlib #operator
Import
import operator
from operator import attrgetter
Signature & Overview
operator.attrgetter(attr, /, *attrs)
Return a callable object that fetches the given attribute(s) from its operand.
Usage
import operator
from types import SimpleNamespace
users = [SimpleNamespace(name="Bob", age=30), SimpleNamespace(name="Ada", age=25)]
assert [u.name for u in sorted(users, key=operator.attrgetter("age"))] == ["Ada", "Bob"]
Gotchas
- Supports dotted paths (
"address.city") and multiple attrs (attrgetter("last", "first")).
REPL Inspect
from operator import attrgetter
help(attrgetter)
Linked from
- tuts/python3/stdlib_module/operator
- tuts/python3/stdlib_module/operator/abs
- tuts/python3/stdlib_module/operator/add
- tuts/python3/stdlib_module/operator/and_
- tuts/python3/stdlib_module/operator/call
- tuts/python3/stdlib_module/operator/concat
- tuts/python3/stdlib_module/operator/contains
- tuts/python3/stdlib_module/operator/countOf
- tuts/python3/stdlib_module/operator/delitem
- tuts/python3/stdlib_module/operator/eq
- tuts/python3/stdlib_module/operator/floordiv
- tuts/python3/stdlib_module/operator/ge
- tuts/python3/stdlib_module/operator/getitem
- tuts/python3/stdlib_module/operator/gt
- tuts/python3/stdlib_module/operator/iadd
- tuts/python3/stdlib_module/operator/iand
- tuts/python3/stdlib_module/operator/iconcat
- tuts/python3/stdlib_module/operator/ifloordiv
- tuts/python3/stdlib_module/operator/ilshift
- tuts/python3/stdlib_module/operator/imatmul
- tuts/python3/stdlib_module/operator/imod
- tuts/python3/stdlib_module/operator/imul
- tuts/python3/stdlib_module/operator/index
- tuts/python3/stdlib_module/operator/indexOf
- tuts/python3/stdlib_module/operator/inv
- tuts/python3/stdlib_module/operator/invert
- tuts/python3/stdlib_module/operator/ior
- tuts/python3/stdlib_module/operator/ipow
- tuts/python3/stdlib_module/operator/irshift
- tuts/python3/stdlib_module/operator/is_
- tuts/python3/stdlib_module/operator/is_none
- tuts/python3/stdlib_module/operator/is_not
- tuts/python3/stdlib_module/operator/is_not_none
- tuts/python3/stdlib_module/operator/isub
- tuts/python3/stdlib_module/operator/itemgetter
- tuts/python3/stdlib_module/operator/itruediv
- tuts/python3/stdlib_module/operator/ixor
- tuts/python3/stdlib_module/operator/methodcaller
Related notes
- tuts/python3/stdlib_module/operator/abs Linked here; links to this page; shared tags: #operator, #stdlib
- tuts/python3/stdlib_module/operator/add Linked here; links to this page; shared tags: #operator, #stdlib
- tuts/python3/stdlib_module/operator/and_ Linked here; links to this page; shared tags: #operator, #stdlib
- tuts/python3/stdlib_module/operator/call Linked here; links to this page; shared tags: #operator, #stdlib
- tuts/python3/stdlib_module/operator Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/operator/concat links to this page; shared tags: #operator, #stdlib
- tuts/python3/stdlib_module/operator/contains links to this page; shared tags: #operator, #stdlib
- tuts/python3/stdlib_module/operator/countOf links to this page; shared tags: #operator, #stdlib
- tuts/python3/stdlib_module/operator/delitem links to this page; shared tags: #operator, #stdlib
- tuts/python3/stdlib_module/operator/eq links to this page; shared tags: #operator, #stdlib