def log(content):
def deco(func):
def deco1(*args,**kw):
print 'called in $s,%s'%('func.__name__,content)
return func(*args,**kw)
return deco1
return deco
@log('hello,world')
def personate(a,b):
return reduce(lambda x,y:x*y,range(a,b+1))
def deco(func):
def deco1(*args,**kw):
print 'called in $s,%s'%('func.__name__,content)
return func(*args,**kw)
return deco1
return deco
@log('hello,world')
def personate(a,b):
return reduce(lambda x,y:x*y,range(a,b+1))
2016-06-02
def cmp_ignore_case(s1, s2):
if s1.lower()>s2.lower():
return 1
if s1.lower()<s2.lower():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.lower()>s2.lower():
return 1
if s1.lower()<s2.lower():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-06-02
import math
def is_sqr(x):
return int(math.sqrt(x))**2==x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return int(math.sqrt(x))**2==x
print filter(is_sqr, range(1, 101))
2016-06-02
其实可以看出来f是个函数,而不是某个值,所以在fs.append(f)的时候,append的其实是一个函数,但是在这个时候f函数是没有进行任何计算的,而f1,f2,f3=count()是个混合赋值,其实相当于f1,f2,f3=[f,f,f],那么,这个时候就调用到了f,f开始参与计算,但是当f完全计算完之后i的值其实是3,所以最后的结果都是9
2016-06-02
from os.path import isdir, isfile
print isdir(r'/data/webroot/resource/python')
print isfile(r'/data/webroot/resource/python/test.txt')
print isdir(r'/data/webroot/resource/python')
print isfile(r'/data/webroot/resource/python/test.txt')
2016-06-02
import time
def performance(f):
def fn(*args, **kw):
print 'call ' + f.__name__ + '() in '+ time.strftime('%H:%M:%S')
return f(*args, **kw)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def performance(f):
def fn(*args, **kw):
print 'call ' + f.__name__ + '() in '+ time.strftime('%H:%M:%S')
return f(*args, **kw)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2016-05-30