Python Language Partial functions

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

As you probably know if you came from OOP school, specializing an abstract class and use it is a practice you should keep in mind when writing your code.

What if you could define an abstract function and specialize it in order to create different versions of it? Thinks it as a sort of function Inheritance where you bind specific params to make them reliable for a specific scenario.

Syntax

  • partial(function, **params_you_want_fix)

Parameters

Paramdetails
xthe number to be raised
ythe exponent
raisethe function to be specialized

Remarks

As stated in Python doc the functools.partial:

Return a new partial object which when called will behave like func called with the positional arguments args and keyword arguments keywords. If more arguments are supplied to the call, they are appended to args. If additional keyword arguments are supplied, they extend and override keywords.

Check this link to see how partial can be implemented.



Got any Python Language Question?