from functools import partial
i = 0
f0 = partial(callback, i)
i = 1
f1 = partial(callback, i)
f0()
# 0
f1()
# 1
partial is like a lambda but wraps the value at that moment into the arg. Not evaluating it when its called.
Wrapping only some of the args
Yes partial will allow you to wrap any number of the arguments, and the remaining args and kwargs can then be passed to the resulting partial object so that it acts like it was calling the original wrapped function...
def callback(val1, val2):
print "{0} {1}".format(val1, val2)
i = 0
x = 8
f0 = partial(callback, i)
f0(x)
# 0 8
Essentially you have wrapped callback(val1, val2) into callback(val2) with val1 being included as a closure already.
Example of similar effect using lambda
In case you really want to see how to do this with a lambda closure, you can see why it gets ugly and partial is preferred...
f0 = (lambda val1: lambda val2: callback(val1, val2))(i)
You have to wrap the scope variable into an outer function scope, and then reference that scope in the inner lambda function. Yuk.
Tracebacks from exceptions: partial vs lambda vs nested functions
With the influx of other answers, I thought I would outline one more reason to use partial as opposed to lambda, or a inner/outer function closure. Keep in mind I mean a function closure. functools.partial fixes the traceback you will get when your wrapped function raises an exception...
Consider this version that will raise a division by zero:
def callback(val1, val2):
return val1 / val2
Normal outter/inner closure
def wrapper(fn, val1):
def wrapped(val2):
return fn(val1, val2)
return wrapped
f0 = wrapper(callback, i)
f0(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in wrapped
File "<stdin>", line 2, in callback
ZeroDivisionError: integer division or modulo by zero
lambda closure
f0 = (lambda val1: lambda val2: callback(val1, val2))(i)
f0(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
File "<stdin>", line 2, in callback
ZeroDivisionError: integer division or modulo by zero
And now for functools.partial
f0 = partial(callback, i)
f0(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in callback
ZeroDivisionError: integer division or modulo by zero
ipartial.