Operator Classes

operator.attrgetter()

Build a callable that retrieves one attribute or a tuple of attributes. Dotted names traverse nested attributes. Useful as a sorting key.

Formal Syntax & Structure

python syntaxpython
operator.attrgetter(attr, /, *attrs)

Parameters & Attributes

NameTypeDescription
attrstrAttribute name, optionally dotted.
attrsstrAdditional attribute names.

Return Value

callable

The callable result described above.

Working Example

Example Snippetpython
import operatorfrom types import SimpleNamespacerecord=SimpleNamespace(meta=SimpleNamespace(score=8), name="Ada")print(operator.attrgetter("name","meta.score")(record))
Python Reference