In Python, suppose I have a function f
that I want to pass around with some secondary arguments (assume for simplicity that it's just the first argument that remains variable).
What are the differences between doing it these two ways (if any)?
# Assume secondary_args and secondary_kwargs have been defined
import functools
g1 = functools.partial(f, *secondary_args, **secondary_kwargs)
g2 = lambda x: f(x, *secondary_args, **secondary_kwargs)
In the doc page for partial
, for example, there is this quote:
partial
objects defined in classes behave like static methods and do not transform into bound methods during instance attribute look-up.
Will the lambda-method suffer from this if used to make a class method from arguments supplied to the class (either in the constructor or through a function later on)?
A lambda function has the same type as a standard function, so it will behave like an instance method.
The
partial
object in your example can be called like this:leading to this call (not valid Python syntax, but you get the idea):
The lambda only accepts a single argument and uses a different argument order. (Of course both of these differences can be overcome – I'm just answering what the differences between the two versions you gave are.)
Execution of the
partial
object is slightly faster than execution of the equivalentlambda
.partials are not only about 20% faster than equivalent lambdas as already said but they keep a direct ref to they function the relate to. While in lambdas that function is 'burried' within the function body.
=> If you need to only solve the problem of defering evaluation of one function until all args are known then use partials. You'll have way better introspection methods compared to bury the calls into anonymous functions, i.e. lambdas.
I believe that the class method thing only applies to functions assigned during class definition. Functions assigned later are not treated specially.
Other than that, I'd personally favor lambdas since they're more common and hence make the code easier to understand.
Yes,
lambda
will "suffer" from this.partial
doesn't have this problem because it is an object with the call operator overloaded, rather than a real function.But using a lambda like this in a class definition is just misuse.