How does functools partial do what it does?
partial does something like this (apart from keyword args support etc):
def partial(func, *part_args): def wrapper(*extra_args): args = list(part_args) args.extend(extra_args) return func(*args) return wrapper
So, by calling
partial(sum2, 4) you create a new function (a callable, to be precise) that behaves like
sum2, but has one positional argument less. That missing argument is always substituted by
4, so that
partial(sum2, 4)(2) == sum2(4, 2)
As for why it's needed, there's a variety of cases. Just for one, suppose you have to pass a function somewhere where it's expected to have 2 arguments:
class EventNotifier(object): def __init__(self): self._listeners =  def add_listener(self, callback): ''' callback should accept two positional arguments, event and params ''' self._listeners.append(callback) # ... def notify(self, event, *params): for f in self._listeners: f(event, params)
But a function you already have needs access to some third
context object to do its job:
def log_event(context, event, params): context.log_event("Something happened %s, %s", event, params)
So, there are several solutions:
A custom object:
class Listener(object): def __init__(self, context): self._context = context def __call__(self, event, params): self._context.log_event("Something happened %s, %s", event, params) notifier.add_listener(Listener(context))
log_listener = lambda event, params: log_event(context, event, params)notifier.add_listener(log_listener)
context = get_context() # whatevernotifier.add_listener(partial(log_event, context))
Of those three,
partial is the shortest and the fastest.(For a more complex case you might want a custom object though).
partials are incredibly useful.
For instance, in a 'pipe-lined' sequence of function calls (in which the returned value from one function is the argument passed to the next).
Sometimes a function in such a pipeline requires a single argument, but the function immediately upstream from it returns two values.
In this scenario,
functools.partial might allow you to keep this function pipeline intact.
Here's a specific, isolated example: suppose you want to sort some data by each data point's distance from some target:
# create some dataimport random as RNDfnx = lambda: RND.randint(0, 10)data = [ (fnx(), fnx()) for c in range(10) ]target = (2, 4)import mathdef euclid_dist(v1, v2): x1, y1 = v1 x2, y2 = v2 return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
To sort this data by distance from the target, what you would like to do of course is this:
but you can't--the sort method's key parameter only accepts functions that take a single argument.
euclid_dist as a function taking a single parameter:
from functools import partialp_euclid_dist = partial(euclid_dist, target)
p_euclid_dist now accepts a single argument,
3, 3)) 1.4142135623730951p_euclid_dist((
so now you can sort your data by passing in the partial function for the sort method's key argument:
data.sort(key=p_euclid_dist)# verify that it works:for p in data: print(round(p_euclid_dist(p), 3)) 1.0 2.236 2.236 3.606 4.243 5.0 5.831 6.325 7.071 8.602
Or for instance, one of the function's arguments changes in an outer loop but is fixed during iteration in the inner loop. By using a partial, you don't have to pass in the additional parameter during iteration of the inner loop, because the modified (partial) function doesn't require it.
from functools import partialdef fnx(a, b, c): return a + b + c fnx(3, 4, 5) 12
create a partial function (using keyword arg)
12) pfnx(b=4, c=5) 21pfnx = partial(fnx, a=
you can also create a partial function with a positional argument
12) pfnx(4, 5) 21pfnx = partial(fnx,
but this will throw (e.g., creating partial with keyword argument then calling using positional arguments)
12) pfnx(4, 5) Traceback (most recent call last): File "<pyshell#80>", line 1, in <module> pfnx(4, 5) TypeError: fnx() got multiple values for keyword argument 'a'pfnx = partial(fnx, a=
another use case: writing distributed code using python's
multiprocessing library. A pool of processes is created using the Pool method:
import multiprocessing as MP# create a process pool:ppool = MP.Pool()
Pool has a map method, but it only takes a single iterable, so if you need to pass in a function with a longer parameter list, re-define the function as a partial, to fix all but one:
map(pfnx, [4, 6, 7, 8])ppool.
partial gives default values to the parameters of a function that would otherwise not have default values.
from functools import partialdef foo(a,b): return a+bbar = partial(foo, a=1) # equivalent to: foo(a=1, b)bar(b=10)#11 = 1+10bar(a=101, b=10)#111=101+10