Picture of Aravind Ramalingam

Aravind Ramalingam

  • Python
  • Partial Function
  • Matplotlib

Customize or Extend Python Third-Party Modules Using Partial Function

Let me explain with an example.

Introduction

Partial functions allow us to fix values for some arguments of a function and generate a new function. The common example for partial function would be:

This example is a solid starter example for partial functions, but feels more like a syntactic sugar rather than functionality gain. Partial function shines, when you are restricted by third party modules or code which you have no control over.

Example Brief

I was playing around with Matplotlib Animation module and found that save method has a progress callback function. Callback function takes two arguments (current_frame & total_frames), but a progress bar without metadata like file name and elapsed time is not very useful. So we will use Partial Function to enhance it.

Animation
Save Function

Creating Animated Line Chart

Write to disk

Progress bar created with help of
partial function

Takeaway

Partial functions give you more control, especially when you are adding features on top of code written by others. If you have any questions or thoughts, feel free to reach out via Contact

Resource

**Animations with Matplotlib