import time, functools
def performance(unit):
start = time.time()
def perf_decorator(f):
@functools.wraps(f)
def a(*args, **kw):
print '[%s] %s()...' % (unit,f.__name__),time.time()-start
return f(*args,**kw)
return a
return perf_decorator
def performance(unit):
start = time.time()
def perf_decorator(f):
@functools.wraps(f)
def a(*args, **kw):
print '[%s] %s()...' % (unit,f.__name__),time.time()-start
return f(*args,**kw)
return a
return perf_decorator
2018-01-23
class Person(object):
__count = 0
def __init__(self, name):
self.name=name
Person.__count=Person.__count+1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print p1.__count+p2.__count
except AttributeError:
print "attributeerror"
__count = 0
def __init__(self, name):
self.name=name
Person.__count=Person.__count+1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print p1.__count+p2.__count
except AttributeError:
print "attributeerror"
2018-01-23
print 'call %s() in %fs' % (f.__name__, (t2 - t1))这句不懂啊
2018-01-23
def format_name(s):
s = s.replace(' ','')
return s[0].upper() + s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT',' sadOIdaD as'])
s = s.replace(' ','')
return s[0].upper() + s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT',' sadOIdaD as'])
2018-01-23
import functools
sorted_ignore_case = functools.partial(sorted,key=str.lower)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
sorted_ignore_case = functools.partial(sorted,key=str.lower)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
2018-01-23
import time
def performance(unit):
def mid(f):
def ins(*args, **kw):
print 'call%s()... %s%s' % (f.__name__,time.time(),unit)
return f(*args, **kw)
return ins
return mid
@performance('ms')
后面是题目里默认的那堆代码
def performance(unit):
def mid(f):
def ins(*args, **kw):
print 'call%s()... %s%s' % (f.__name__,time.time(),unit)
return f(*args, **kw)
return ins
return mid
@performance('ms')
后面是题目里默认的那堆代码
2018-01-23
def calc_prod(lst):
def lazy_calc():
return functools.reduce(lambda x,y : x*y,lst)
return lazy_calc
a = [1,2,3,4]
print(calc_prod(a))
print(calc_prod(a)())
<function calc_prod.<locals>.lazy_calc at 0x0000000004E1ABF8>
24
def lazy_calc():
return functools.reduce(lambda x,y : x*y,lst)
return lazy_calc
a = [1,2,3,4]
print(calc_prod(a))
print(calc_prod(a)())
<function calc_prod.<locals>.lazy_calc at 0x0000000004E1ABF8>
24
2018-01-23