Skip to content

Instantly share code, notes, and snippets.

@matchu
Created November 13, 2012 02:32
Show Gist options
  • Save matchu/4063617 to your computer and use it in GitHub Desktop.
Save matchu/4063617 to your computer and use it in GitHub Desktop.
Python pop quiz: closures solution
def build_multiplier(i):
return (lambda n: n * i)
multipliers = []
for i in range(3):
multipliers.append(build_multiplier(i))
# It do what you think.
print multipliers[0](10)
print multipliers[1](10)
print multipliers[2](10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment