def performance(unit):
def per_decorator(f):
def fn(*args, **kw):
st = time.time()
f(*args, **kw)
et = time.time()
def per_decorator(f):
def fn(*args, **kw):
st = time.time()
f(*args, **kw)
et = time.time()
2017-12-06
if unit == 's':
print 'call %s() in %s' %(f.__name__,et-st)
else:
print 'call %s() in %s' %(f.__name__,(et-st)*1000)
return f(*args, **kw)
return fn
return per_decorator
print 'call %s() in %s' %(f.__name__,et-st)
else:
print 'call %s() in %s' %(f.__name__,(et-st)*1000)
return f(*args, **kw)
return fn
return per_decorator
2017-12-06
def performance(f):
def fn(*args, **kw):
st = time.time()
f(*args, **kw)
et = time.time()
print 'call %s() in %s' %(f.__name__,et-st)
return f(*args, **kw)
return fn
def fn(*args, **kw):
st = time.time()
f(*args, **kw)
et = time.time()
print 'call %s() in %s' %(f.__name__,et-st)
return f(*args, **kw)
return fn
2017-12-06
return reduce(f, lst, 1)如果写成 return reduce(f, lst,),就是默认lst的第一位数值作为初始值计算吧
2017-12-06
ef calc_prod(lst):
def lazy_prod():
def f(x, y):
return x * y
return reduce(f, lst, 1)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
其中的 return reduce(f, lst, 1)里面的1,是指把初始值设置为1
def lazy_prod():
def f(x, y):
return x * y
return reduce(f, lst, 1)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
其中的 return reduce(f, lst, 1)里面的1,是指把初始值设置为1
2017-12-06
class Person(object):
def __init__(self, name, score):
self.name=name
self.__score=score
p = Person('Bob', 59)
print p.name
print p.__score
def __init__(self, name, score):
self.name=name
self.__score=score
p = Person('Bob', 59)
print p.name
print p.__score
2017-12-06
sorted_ignore_case = functools.partial(sorted,key=lambda x:x.lower())
2017-12-05
def cmp_ignore_case(s1, s2):
return (-1 if s1.upper() < s2.upper() else 1)
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
return (-1 if s1.upper() < s2.upper() else 1)
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2017-12-05
def count():#定义函数名
fs = []#定义函数体
for i in range(1, 4):#打印序列,for i in range(x,y,z)x是开始值,y结束值,z是步长,默认为1
def f(j):
def g():
return j*j#结束函数
return g
r = f(i)
fs.append(r)#保存
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []#定义函数体
for i in range(1, 4):#打印序列,for i in range(x,y,z)x是开始值,y结束值,z是步长,默认为1
def f(j):
def g():
return j*j#结束函数
return g
r = f(i)
fs.append(r)#保存
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2017-12-05
def format_name(s):
return s.lower() and s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.lower() and s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2017-12-05
class Person(object):
def __init__(self,name,gender,birth,**kwargs):
self.name=name
self.gender=gender
self.birth=birth
for key,value in kwargs.items():
setattr(self,key,value)
重点:kwargs.items()、 setattr函数
def __init__(self,name,gender,birth,**kwargs):
self.name=name
self.gender=gender
self.birth=birth
for key,value in kwargs.items():
setattr(self,key,value)
重点:kwargs.items()、 setattr函数
2017-12-05