Let’s understand Functors First:
Functors are objects the that can be treated as though they are a function.
When to use functors?
- Functors are used when you want to hide/abstract the real implementation. Let’s say you want to call the different functions depending on the input but you don’t want the user code to make explicit calls to those different functions. This is the ideal situation where functors can help.
- In this scenario, we can go for a functor which internally calls the most suitable function depending on the input
- Now if later, none of functions to be called increases, then it would be just a simple change in the backend code without disturbing any of the user code. Thus functors help in creating maintainable, decoupled and extendable codes.
Let’s understand it by a simple design problem example. The problem is to design class/method which will call different sorting method based on the input type. If the input is of type int then Mergesort function should be called and if the input is of type float then Heapsort otherwise just call quicksort function
Data is Merge sorted [1, 2, 3]
There are some evident design gaps in this code
1. Inner Implementation should be hidden from the user code i.e abstraction should be maintained
2. Every class should handle single responsibility/functionality.
2. The code is tightly coupled.
Let’s solve the same problem using functors in python
Data is Merge sorted [5, 4, 6] Data is Heap sorted [2.23, 3.45, 5.65] Data is Quick sorted ['a', 's', 'b', 'q']
The above design makes it easy to change the underneath strategy or implementation without disturbing any user code. Usercode can reliably use the above functor without knowing what is going underneath the hood,
making the code decoupled, easily extendable and maintainable.
Now, along with the functions in the python, you have also understood the strategy pattern in Python which calls for the separation between the Class calling the specific function and Class where strategies are listed or chosen.
This article is contributed by Ankit Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Use of nonlocal vs use of global keyword in Python
- Use of min() and max() in Python
- How to use close() and quit() method in Selenium Python ?
- How to use NamedTuple and Dataclass in Python?
- isupper(), islower(), lower(), upper() in Python and their applications
- When to use yield instead of return in Python?
- Python | Use of __slots__
- How to use a List as a key of a Dictionary in Python 3?
- Use Pandas to Calculate Statistics in Python
- Python | How to use Multiple kv files in kivy
- How to use multiple UX Widgets in kivy | Python
- Use of slice() in Python
- How to use sys.argv in Python
- How to use Glob() function to find files recursively in Python?
- Python program to find GSoC organisations that use a Particular Programming Language
- Python theHarvester - How to use it?
- Use get() method to create a dictionary in Python from a list of elements
- Use of na_values parameter in read_csv() function of Pandas in Python
- How to use Color Palettes in Python-Bokeh?
- How to use datasets.fetch_mldata() in sklearn - Python?