Created
November 13, 2012 02:32
-
-
Save matchu/4063617 to your computer and use it in GitHub Desktop.
Python pop quiz: closures solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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