The functools.partial allow to wrapped a method with anew method that has only partial of the parameters of the wrapped function .
In constract to using lambda for this ‘the functools.partial keep the parameters as reference that can be changed after declaring the wrapper function
Example:
import functoolsfullName = lambda pName , pFamily : pName + " "+ pFamilyn = "Zvika"GetName = lambda y: fullName(n, y)GetName2 = functools.partial(fullName, n)print ("n set before creating the lambda")print (GetName("Peer"), GetName2("Peer"))print ("n set after creating the lambda")n = "Alon"print (GetName("Peer"), GetName2("Peer"))
And the results:
n set before creating the lambda
Zvika Peer Zvika Peer
n set after creating the lambda
Alon Peer Zvika Peer
Resources:
http://stackoverflow.com/questions/3252228/python-why-is-functools-partial-necessary
אין תגובות:
הוסף רשומת תגובה