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
| Name | Type | Description |
|---|---|---|
| attr | str | Attribute name, optionally dotted. |
| attrs | str | Additional 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))