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)?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…