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:

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:

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 object with the updated salary.
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal