O'Reilly logo
  • Chris Kang thinks this is interesting:

def make_adder_as_closure(augend): def add(addend, _augend=augend): return addend+_augend return add


Cover of Python in a Nutshell, 3rd Edition


Returns a wrapper to the add() method as a closure object. It remembers what was passed into "augend" argument when this closure was initially created.

a = make_adder_as_closure(1) a(2) # print the result of 2 + 1