def count():
fs = []
for i in range(1, 4):
def f1(i):
def f2():
return i * i
return f2
fs.append(f1(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f1(i):
def f2():
return i * i
return f2
fs.append(f1(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-03-03
def performance(f):
def fn(n):
t_start = time.time()
r = f(n)
t_stop = time.time()
print 'call ' + f.__name__ + '() in ' + str(t_stop - t_start)
return r
return fn
def fn(n):
t_start = time.time()
r = f(n)
t_stop = time.time()
print 'call ' + f.__name__ + '() in ' + str(t_stop - t_start)
return r
return fn
2015-03-03
最赞回答 / 歪歪的酒壶
从fs.append(f)语句可以看出,函数count实际上返回的是一个列表,然后将这个列表负值给f1, f2, f3如果在return fs语句之前增加一个print fs,就能够清晰的看出返回的是一个列表啦[<function f at 0x0000000002191978>, <function f at 0x00000000021919E8>, <function f at 0x0000000002191A58>]
2015-03-03
def cmp_ignore_case(s1, s2):
if s1.lower() < s2.lower():
return -1
elif s1.lower() > s2.lower():
return 1
else:
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.lower() < s2.lower():
return -1
elif s1.lower() > s2.lower():
return 1
else:
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2015-03-03