def count():
fs = []
for i in range(1, 4):
def shi(i):
def ji():
return i * i
return ji
fs.append(shi(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def shi(i):
def ji():
return i * i
return ji
fs.append(shi(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-08-31
这样理解也可以:
```python
def new_f(f):
print 'call ' + f.__name__ + '()'
return f
```
```python
def new_f(f):
print 'call ' + f.__name__ + '()'
return f
```
2015-08-31
def performance(prefix):
def fix_perf(f):
def fn(*args,**kw):
t1=time.time()
fx=f(*args,**kw)
t2=time.time()
print 'call '+f.__name__+'() in %f%s' %(t2-t1,prefix)
return f(*args,**kw)
return fn
return fix_perf
def fix_perf(f):
def fn(*args,**kw):
t1=time.time()
fx=f(*args,**kw)
t2=time.time()
print 'call '+f.__name__+'() in %f%s' %(t2-t1,prefix)
return f(*args,**kw)
return fn
return fix_perf
2015-08-31
import time
def performance(f):
def fn(*args,**kw):
t1=time.time()
fx=f(*args,**kw)
t2=time.time()
print 'call '+f.__name__+'() in %fs' %(t2-t1)
return f(*args,**kw)
return fn
def performance(f):
def fn(*args,**kw):
t1=time.time()
fx=f(*args,**kw)
t2=time.time()
print 'call '+f.__name__+'() in %fs' %(t2-t1)
return f(*args,**kw)
return fn
2015-08-31
def count():
fs = []
for i in range(1, 4):
def f(x):
def g():
return x*x
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(x):
def g():
return x*x
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-08-31
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
2015-08-31
def calc_prod(lst):
def prod(x,y):
return x*y
def lazy_prod():
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod(x,y):
return x*y
def lazy_prod():
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2015-08-31
lambda p1, p2: cmp(p1.name, p2.name) 是匿名函数的用法
p1,p2是传入参数,cmp(p1.name,p2.name)是返回值(2-10节的那个实例)
这里猜想cmp函数是用来区分大小写的,不过好像没有实现
不用区分大小写的话这样写就可以了:L2 = sorted(L1,key = lambda x:x.name)
p1,p2是传入参数,cmp(p1.name,p2.name)是返回值(2-10节的那个实例)
这里猜想cmp函数是用来区分大小写的,不过好像没有实现
不用区分大小写的话这样写就可以了:L2 = sorted(L1,key = lambda x:x.name)
2015-08-30