getters

Variants of operator.attrgetter(), operator.itemgetter() and operator.methodcaller() which operate on values within sequences.

New in version 3.2.0.

Classes:

attrgetter(idx, attr)

Returns a callable object that fetches attr from the item at index idx in its operand.

itemgetter(idx, item)

Returns a callable object that fetches item from the item at index idx in its operand, using the __getitem__() method.

methodcaller(_idx, _name, *args, **kwargs)

Returns a callable object that calls the method name on the item at index idx in its operand.

class attrgetter(idx, attr)[source]

Returns a callable object that fetches attr from the item at index idx in its operand.

The attribute name can contain dots. For example:

  • After f = attrgetter(0, 'name'), the call call f(b) returns b[0].name.

  • After f = attrgetter(3, 'name.first'), the call f(b) returns b[3].name.first.

>>> from pathlib import Path
>>> attrgetter(0, 'name')([Path("dir/code.py")])
'code.py'
>>> attrgetter(2, 'parent.name')([Path("dir/coincidence.py"), Path("dir/wheel.py"), Path("dir/operator.py")])
'dir'
Parameters
  • idx (int) – The index of the item to obtain the attribute from.

  • attr (str) – The name of the attribute.

class itemgetter(idx, item)[source]

Returns a callable object that fetches item from the item at index idx in its operand, using the __getitem__() method.

For example:

  • After f = itemgetter(0, 2), the call call f(r) returns r[0][2].

  • After g = itemgetter(3, 5), the call g(r) returns r[3][5].

The items can be any type accepted by the item’s __getitem__() method. Dictionaries accept any hashable value. Lists, tuples, and strings accept an index or a slice:

>>> itemgetter(0, 1)(['ABCDEFG'])
'B'
>>> itemgetter(1, 2)(['ABC', 'DEF'])
'F'
>>> itemgetter(0, slice(2, None))(['ABCDEFG'])
'CDEFG'
>>> army = [dict(rank='captain', name='Blackadder'), dict(rank='Private', name='Baldrick')]
>>> itemgetter(0, 'rank')(army)
'captain'
Parameters
  • idx (int) – The index of the item to call __getitem__() on.

  • item (Any) – The value to pass to __getitem__().

class methodcaller(_idx, _name, *args, **kwargs)[source]

Returns a callable object that calls the method name on the item at index idx in its operand.

If additional arguments and/or keyword arguments are given, they will be passed to the method as well. For example:

  • After f = methodcaller(0, 'name'), the call f(b) returns b[0].name().

  • After f = methodcaller(1, 'name', 'foo', bar=1), the call f(b) returns b[1].name('foo', bar=1).

>>> from datetime import date
>>> methodcaller(0, 'upper')(["hello", "world"])
'HELLO'
>>> methodcaller(1, 'center', 9, "=")(["hello", "world"])
'==world=='
>>> methodcaller(0, 'replace', year=2019)([date(2021, 7, 6)])
datetime.date(2019, 7, 6)
Parameters
  • _idx – The index of the item to call the method on.

  • _attr – The name of the method to call.

  • *args – Positional arguments to pass to the method.

  • **kwargs – Keyword arguments to pass to the method.