Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is the operator.methodcaller method in Python?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The operator module is a built-in module in Python. The module provides functions equivalent to the intrinsic operators of Python. For instance, we can use the operator.mul() method for multiplication instead of using the multiplication (*) operator.

The module’s functions come in handy when callable methods are given as arguments to another Python object.

The methodcaller() method

The methodcaller() method returns a callable object that calls the method name on its operand. It can call a method on an object by providing its name supplied as a string. Additional arguments and keyword arguments can also be passed on to the method.

Syntax

operator.methodcaller(name, /, *args, **kwargs)

Parameters

  • name: This is the method name.
  • args and kwargs: These are method arguments.

Return value

This method returns a callable object that calls the method name on its operand.

Example

from operator import methodcaller

lst_pop = methodcaller('pop')
lst = [1,2,3,4]
popped_element = lst_pop(lst)
print("%s.pop() = %s" % (lst, popped_element))

element = 3
lst_count = methodcaller('count', element)
lst = [1, 2, 2, 3, 3, 3]
count_of_element = lst_count(lst)
print("%s.count(%s) = %s" % (lst, element, count_of_element))

Explanation

  • Line 1: We import the methodcaller method from the operator module.
  • Line 3: We obtain a callable for the pop function, lst_pop, of the list is obtained using methodcaller. The pop method doesn’t take arguments. Hence, no arguments are given in the invocation of the methodcaller.
  • Line 4: We define a list of numbers called lst.
  • Line 5: We invoke a lst_pop callable with lst as the argument. The return value is the last element popped from lst.
  • Line 6: We print the popped element along with lst.
  • Line 8: We define an integer value called element.
  • Line 9: We obtain a callable for the count function called lst_count of the list using methodcaller. The count method takes an argument, that is the element to count in the list. Hence, element is given during the invocation of the methodcaller.
  • Line 10: We define a list of numbers with duplicates called lst.
  • Line 11: We invoke a lst_count callable with lst as the argument. The return value is the number of occurrences of element in lst.
  • Line 12: We print the frequency of element along with lst.

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring