def performance(unit):
def decorator(f):
def wrapper(*args, **kw):
print 'call ' + f.__name__ + '()'
return f(*args, **kw)
return wrapper
return decorator
def decorator(f):
def wrapper(*args, **kw):
print 'call ' + f.__name__ + '()'
return f(*args, **kw)
return wrapper
return decorator
2017-08-20
让人看懂~
def calc_prod(lst):
def w():
s = lst[0]
for num in lst:
s = s * num
return s/lst[0]
return w
f = calc_prod([1, 2, 3, 4])
print f()
def calc_prod(lst):
def w():
s = lst[0]
for num in lst:
s = s * num
return s/lst[0]
return w
f = calc_prod([1, 2, 3, 4])
print f()
2017-08-20
import time
def performance(f):
def fn(*args, **kw):
print 'call ' + f.__name__ + '() in' + str(time.time())
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' + str(time.time())
return f(*args, **kw)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2017-08-20
def count():
fs = []
for i in range(1, 4):
def f():
x = i * i
return lambda:x
fs.append(f())
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f():
x = i * i
return lambda:x
fs.append(f())
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2017-08-20
def calc_prod(lst):
def calc_prod2():
return reduce(lambda x, y: x * y, lst)
return calc_prod2
f = calc_prod([1, 2, 3, 4])
print f()
def calc_prod2():
return reduce(lambda x, y: x * y, lst)
return calc_prod2
f = calc_prod([1, 2, 3, 4])
print f()
2017-08-20
class Person(object):
count = 0
def __init__(self,name):
self.name = name
Person.count += 1
p1 = Person('Bob')
print Person.count,
p2 = Person('Alice')
print Person.count,
p3 = Person('Tim')
print Person.count
count = 0
def __init__(self,name):
self.name = name
Person.count += 1
p1 = Person('Bob')
print Person.count,
p2 = Person('Alice')
print Person.count,
p3 = Person('Tim')
print Person.count
2017-08-19
我感觉带参数的装饰器,就是多加了一层返回函数,并且闭包。和前面理解起来都一样。然后答案自己也写出来了
def performance(unit):
def decorator(func):
def wrapper(*arg, **kw):
print "call"+func.__name__+"()"+str(time.time())+"%s" %unit
return func(*arg, **kw)
return wrapper
return decorator
def performance(unit):
def decorator(func):
def wrapper(*arg, **kw):
print "call"+func.__name__+"()"+str(time.time())+"%s" %unit
return func(*arg, **kw)
return wrapper
return decorator
2017-08-18
import time
ISOTIMEFORMAT = '%Y-%m-%d %X'#time
def performance(f):
def inner(x):
print 'call',f.__name__+'()','in',time.strftime(ISOTIMEFORMAT,time.localtime())
return f(x)
return inner
ISOTIMEFORMAT = '%Y-%m-%d %X'#time
def performance(f):
def inner(x):
print 'call',f.__name__+'()','in',time.strftime(ISOTIMEFORMAT,time.localtime())
return f(x)
return inner
2017-08-18
可以去廖老师的网站看一看,那里讲得更详细。
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431865288798deef438d865e4c2985acff7e9fad15e3000#0
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431865288798deef438d865e4c2985acff7e9fad15e3000#0
2017-08-18
def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
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(j):
def g():
return j*j
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2017-08-18
print L2[0].name,
print L2[1].name,
print L2[2].name
print后面要加逗号
print L2[1].name,
print L2[2].name
print后面要加逗号
2017-08-18