partial-application

Python: Why is functools.partial necessary?

五迷三道 提交于 2019-11-26 00:30:06
问题 Partial application is cool. What functionality does functools.partial offer that you can\'t get through lambdas? >>> sum = lambda x, y : x + y >>> sum(1, 2) 3 >>> incr = lambda y : sum(1, y) >>> incr(2) 3 >>> def sum2(x, y): return x + y >>> incr2 = functools.partial(sum2, 1) >>> incr2(4) 5 Is functools somehow more efficient, or readable? 回答1: What functionality does functools.partial offer that you can't get through lambdas? Not much in terms of extra functionality (but, see later) -- and,