python functool.partial

Creator
Creator
Seonglae Cho
Created
Created
2023 Nov 30 14:48
Editor
Edited
Edited
2024 Oct 18 23:4
Refs
Refs

Make partial function

functools.partial is a utility in Python's functools module that allows you to create a new function with some predefined arguments of an existing function. It's useful when you have a function that you commonly call with the same parameters and want to avoid repetitive code.
from functools import partial, lru_cache import numpy as np @lru_cache(maxsize=128) def expensive_computation(x, y): return np.sum(np.power(np.arange(x), y)) partial_compute = partial( expensive_computation, y=2) # Usage result1 = partial_compute(1000000) result2 = partial_compute(1000000) #Cached result print(f"Result 1: {result1}") print(f"Result 2: {result2}") # Result 1: 333332833333500000 # Result 2: 333332833333500000
 
 
 
 
 
 
 

Recommendations