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-09-02
import math
def is_sqr(x):
if( math.sqrt(x) * 10)%10==0:
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
if( math.sqrt(x) * 10)%10==0:
return x
print filter(is_sqr, range(1, 101))
2015-09-02
def cmp_ignore_case(s1, s2):
if s1[0].upper() > s2[0].upper():
return 1
else:
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1[0].upper() > s2[0].upper():
return 1
else:
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2015-09-02
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