Functors and their use in Python
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 email@example.com. 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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course