Customize or Extend Python Third-Party Modules Using Partial Function

Let me explain with an example.IntroductionPartial 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:https://medium.com/media/6a30c982d7a3aebac07ca1430f2888f2…


This content originally appeared on Level Up Coding - Medium and was authored by Aravind Ramalingam

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.

Resource

Animations with Matplotlib


Customize or Extend Python Third-Party Modules Using Partial Function was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Aravind Ramalingam


Print Share Comment Cite Upload Translate Updates
APA

Aravind Ramalingam | Sciencx (2021-07-10T22:13:02+00:00) Customize or Extend Python Third-Party Modules Using Partial Function. Retrieved from https://www.scien.cx/2021/07/10/customize-or-extend-python-third-party-modules-using-partial-function/

MLA
" » Customize or Extend Python Third-Party Modules Using Partial Function." Aravind Ramalingam | Sciencx - Saturday July 10, 2021, https://www.scien.cx/2021/07/10/customize-or-extend-python-third-party-modules-using-partial-function/
HARVARD
Aravind Ramalingam | Sciencx Saturday July 10, 2021 » Customize or Extend Python Third-Party Modules Using Partial Function., viewed ,<https://www.scien.cx/2021/07/10/customize-or-extend-python-third-party-modules-using-partial-function/>
VANCOUVER
Aravind Ramalingam | Sciencx - » Customize or Extend Python Third-Party Modules Using Partial Function. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/10/customize-or-extend-python-third-party-modules-using-partial-function/
CHICAGO
" » Customize or Extend Python Third-Party Modules Using Partial Function." Aravind Ramalingam | Sciencx - Accessed . https://www.scien.cx/2021/07/10/customize-or-extend-python-third-party-modules-using-partial-function/
IEEE
" » Customize or Extend Python Third-Party Modules Using Partial Function." Aravind Ramalingam | Sciencx [Online]. Available: https://www.scien.cx/2021/07/10/customize-or-extend-python-third-party-modules-using-partial-function/. [Accessed: ]
rf:citation
» Customize or Extend Python Third-Party Modules Using Partial Function | Aravind Ramalingam | Sciencx | https://www.scien.cx/2021/07/10/customize-or-extend-python-third-party-modules-using-partial-function/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.