Hello! In our journey through functional programming, we've explored currying, partial application, and callable objects. Today, we'll dive into an advanced example using callable objects in combination with Python's powerful higher-order functions and list comprehensions. The goal is to deepen your understanding of how to create and utilize callable objects to make your code more modular and reusable, especially in a real-world context like adjusting employee salaries.
By the end of this lesson, you'll be able to create complex callable objects, apply them to collections using higher-order functions, and understand the benefits of such an approach.
We start by defining a class for employees
:
This class holds basic details about an employee, which include their name and salary.
Let's create a callable class that increases salary by a certain factor:
In this example:
- The constructor
__init__(self, factor)
initializes thefactor
used to increase the salary. - The
__call__
method takes anEmployee
object and returns a new object with the updated salary.
