def performance(unit):
def perf_1(f):
def wrapper(*args,**kw):
print('call '+f.__name__+'() in '+str(f(*args,**kw))+unit)
#return f(*args,**kw)
return wrapper
return perf_1
@performance('ms')
def factorial(n):
return reduce(lambda x,y:x*y,range(1,n+1))
factorial(10)
def perf_1(f):
def wrapper(*args,**kw):
print('call '+f.__name__+'() in '+str(f(*args,**kw))+unit)
#return f(*args,**kw)
return wrapper
return perf_1
@performance('ms')
def factorial(n):
return reduce(lambda x,y:x*y,range(1,n+1))
factorial(10)
2017-08-04
def __cmp__(self, s):
if self.score > s.score:
return -1
elif self.score < s.score:
return 1
elif self.name < s.name:
return -1
elif self.name > s.name:
return 1
else:
return 0
if self.score > s.score:
return -1
elif self.score < s.score:
return 1
elif self.name < s.name:
return -1
elif self.name > s.name:
return 1
else:
return 0
2017-08-04
# for i, j in kw.iteritems():
# setattr(self, i, j)
self.__dict__.update(kw)
# setattr(self, i, j)
self.__dict__.update(kw)
2017-08-04
def jian(a,b):
if a>=b:
for x in range(b+1)[::-1]:
if b%x==0 and a%x==0:
return a//x,b//x
else:
for x in range(a+1)[::-1]:
if b%x==0 and a%x==0:
return a//x,b//x
化简函数
if a>=b:
for x in range(b+1)[::-1]:
if b%x==0 and a%x==0:
return a//x,b//x
else:
for x in range(a+1)[::-1]:
if b%x==0 and a%x==0:
return a//x,b//x
化简函数
2017-08-04
def __cmp__(self, s):
if self.score < s.score:
return 1
elif self.score > s.score:
return -1
else:
return cmp(self.name,s.name)
if self.score < s.score:
return 1
elif self.score > s.score:
return -1
else:
return cmp(self.name,s.name)
2017-08-04
经过装饰器包装的函数,此函数相当于参数传进装饰器中,所以函数的属性会变成装饰器的属性;用@functools.wraps(f) 装饰器,相当于把参数函数的属性赋值给装饰器函数
2017-08-03
self.get_grade = lambda: 'A'是函数,p1.get_grade返回的是一个函数对象,p1.get_grade()才是方法调用
2017-08-03
from functools import reduce
def calc_prod(lst):
def a():
return reduce(lambda x, y: x*y, lst)
return a
f = calc_prod([1, 2, 3, 4])
print(f())
def calc_prod(lst):
def a():
return reduce(lambda x, y: x*y, lst)
return a
f = calc_prod([1, 2, 3, 4])
print(f())
2017-08-03
def __str__(self):
return '(Student: %s,%s,%s)' % (self.name.lower(), self.gender,self.score)
return '(Student: %s,%s,%s)' % (self.name.lower(), self.gender,self.score)
2017-08-03
from __future__ import unicode_literals
s = 'am I an unicode?'
print isinstance(s, unicode)
s = 'am I an unicode?'
print isinstance(s, unicode)
2017-08-03