Created
October 30, 2014 14:06
-
-
Save wenhoujx/50d3b721663ecf62240a to your computer and use it in GitHub Desktop.
closure_2
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
from functools import wraps | |
import pickle | |
import sys | |
def foo(x): | |
def bar(): | |
return x**2 | |
return bar | |
if __name__ == '__main__': | |
try: | |
c = int(raw_input('input 1,2,3 to choose which test to run: ')) | |
except ValueError: | |
print 'not a number' | |
# this will work, because pickle needs a name | |
if c == 1: | |
print 'test1' | |
bar = foo(2) | |
with open('./delme', 'w') as ff: | |
pickle.dump(bar, ff) | |
elif c == 2: | |
print 'test2' | |
# this will not work, because pickle needs a name | |
with open('./delme', 'w') as ff: | |
pickle.dump(foo(2), ff) | |
elif c == 3: | |
print 'test3' | |
# this will not work, because of the name conflict | |
foo = foo(2) | |
with open('./delme', 'w') as ff: | |
pickle.dump(foo, ff) | |
else: | |
print 'input out of range' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment