A function can have multiple parameters, and in some cases some parameters are first obtained, some parameters need to be known in the following scenario, Python provides us with the partial function for carrying some parameters to generate a new function.
def Add (a,b,c=2): print("a is:%s b is%s c is%s"%(a,b,c ) return a+b+cadd_with_a_b=partial (add,2,3)print(add_with_a _b ()# it ' s 7add_with_a=partial (add,9)print(add_with_a)# it ' s 21
#################
A is:2 B is 3 C is 2
7
A Is:9 B is ten C is 2
21st
The following is an alternative to using partial to create an adorner that modifies the function's __doc__ to another function.
#Coding:utf-8" "Created on 2014-10-1@author:zkchen" " fromFunctoolsImportPartialparams=("__doc__",)defUpdate_params (target,source,params=PARAMS):#set Target's property mentioned in the params to source.[SetAttr (Target,p,getattr (Source,p,none)) forPinchPARAMS]returnTargetdefupdate_params_wrap (source):returnPartial (update_params,source=source,params=PARAMS)deftest_partial ():defFunca ():" "This is Funca ' s doc" " Pass@update_params_wrap (Funca)defFUNCB ():" "This is FUNCB ' s doc" " Pass Print(FUNCB.__doc__)
#################################
This is Funca ' s doc
Partial function-python Learning