Lesson 4
Advanced Functional Programming with Callable Objects in Python
Lesson Introduction

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.

Setting Up

We start by defining a class for employees:

Python
1class Employee: 2 def __init__(self, name, salary): 3 self.name = name 4 self.salary = salary

This class holds basic details about an employee, which include their name and salary.

Defining a Functional Object

Let's create a callable class that increases salary by a certain factor:

Python
1class SalaryIncrease: 2 def __init__(self, factor): 3 self.factor = factor 4 5 def __call__(self, employee): 6 return Employee(employee.name, employee.salary * self.factor)

In this example:

  • The constructor __init__(self, factor) initializes the factor used to increase the salary.
  • The __call__ method takes an Employee object and returns a new Employee object with the updated salary.
Using Functional Objects in a Program

Let's integrate the Employee class and the SalaryIncrease callable into a simple program. We need a collection of employees. We'll use a Python list:

Python
1employees = [ 2 Employee("Alice", 30000), 3 Employee("Bob", 45000), 4 Employee("Charlie", 32000), 5 Employee("David", 52000), 6 Employee("Eve", 48000) 7]

To apply the 10% salary increase, we instantiate the SalaryIncrease callable:

Python
1increase_factor = 1.1 # 10% raise 2increase = SalaryIncrease(increase_factor)
Applying the Callable with List Comprehension

We can apply our callable to each employee in the list using the map function:

Python
1increased_salaries = list(map(increase, employees))

The map function applies increase to each object in the employees list.

Printing Results

To print the results, we'll use a simple for loop:

Python
1for emp in increased_salaries: 2 print(f"{emp.name} now earns: {emp.salary}") 3# Output: 4# Alice now earns: 33000.0 5# Bob now earns: 49500.0 6# Charlie now earns: 35200.0 7# David now earns: 57200.0 8# Eve now earns: 52800.0

In this code:

  • The for loop iterates over the transformed list.
  • We print each employee's name and new salary.
Complete Program

Here is the complete program demonstrating all the concepts:

Python
1class Employee: 2 def __init__(self, name, salary): 3 self.name = name 4 self.salary = salary 5 6class SalaryIncrease: 7 def __init__(self, factor): 8 self.factor = factor 9 10 def __call__(self, employee): 11 return Employee(employee.name, employee.salary * self.factor) 12 13 14if __name__ == "__main__": 15 employees = [ 16 Employee("Alice", 30000), 17 Employee("Bob", 45000), 18 Employee("Charlie", 32000), 19 Employee("David", 52000), 20 Employee("Eve", 48000) 21 ] 22 23 increase_factor = 1.1 # 10% raise 24 increase = SalaryIncrease(increase_factor) 25 26 increased_salaries = list(map(increase, employees)) 27 28 for emp in increased_salaries: 29 print(f"{emp.name} now earns: {emp.salary}") 30 # Output: 31 # Alice now earns: 33000.0 32 # Bob now earns: 49500.0 33 # Charlie now earns: 35200.0 34 # David now earns: 57200.0 35 # Eve now earns: 52800.0
Lesson Summary and Practice Introduction

To recap, in this lesson we've:

  • Defined and utilized callable objects in Python to make our code more modular and reusable.
  • Applied these callable objects to collections using list comprehensions to create elegant and readable code.
  • Printed the results using a standard for loop.

Now it's time to get hands-on practice. You'll move to practice sessions where you'll apply these concepts using your own IDE. This will help solidify your understanding and mastery of creating and using callable objects in Python. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.