Differences between functools.partial and a simila

2020-02-05 03:44发布

问题:

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)?

回答1:

  1. A lambda function has the same type as a standard function, so it will behave like an instance method.

  2. The partial object in your example can be called like this:

    g1(x, y, z)
    

    leading to this call (not valid Python syntax, but you get the idea):

    f(*secondary_args, x, y, z, **secondary_kwargs)
    

    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.)

  3. Execution of the partial object is slightly faster than execution of the equivalent lambda.



回答2:

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.

class Foo(object):
    def __init__(self, base):
        self.int = lambda x:int(x, base)

print Foo(4).int('11')


回答3:

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.



回答4:

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.