import time
def performance(unit):
def performance_d(f):
def fn(*args,**kw):
print 'call [%s] in %s (%s)' % (f.__name__,time.time(),unit)
return f(*args,**kw)
return fn
return performance_d
def performance(unit):
def performance_d(f):
def fn(*args,**kw):
print 'call [%s] in %s (%s)' % (f.__name__,time.time(),unit)
return f(*args,**kw)
return fn
return performance_d
2015-04-17
import time
def performance(f):
def fn(*args,**kw):
print 'call factorial() in',time.time()
return f(*args,**kw)
return fn
def performance(f):
def fn(*args,**kw):
print 'call factorial() in',time.time()
return f(*args,**kw)
return fn
2015-04-17
print filter(lambda s: s and len(s.strip())>0, ['test', None, '', 'str', ' ', 'END'])
2015-04-17
def count():
fs = []
for i in range(1, 4):
def f(x=i):
return x*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):
return x*x
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-04-17
def calc_prod(lst):
def chengji():
sum=1
for i in lst:
sum=sum*i
return sum
return chengji
f = calc_prod([1, 2, 3, 4])
print f()
def chengji():
sum=1
for i in lst:
sum=sum*i
return sum
return chengji
f = calc_prod([1, 2, 3, 4])
print f()
2015-04-17
def cmp_ignore_case(s1, s2):
if s1.upper()>s2.upper():
return 1
if s1.upper()<s2.upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.upper()>s2.upper():
return 1
if s1.upper()<s2.upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2015-04-17
import math
def is_sqr(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
2015-04-17
最赞回答 / 不会换气的鱼
那你还是没明白本章在讲什么。本章讲的就是:用装饰器@property 把 get/set 方法“装饰”成属性调用把 方法 装饰成 属性当然,具体的实现细节还得看@property的源码,现在只要知道@property的用途就足够了。
2015-04-16
def f1(f):
@functools.wraps(f)
def f2(*args, **kw):
return f(*args, **kw)
return f2
return f1
@functools.wraps(f)
def f2(*args, **kw):
return f(*args, **kw)
return f2
return f1
2015-04-16
lambda: 'A'等价于return'A',相当于一个函数f,那么f()='A'.因此,p1.get_grade=f,p1.get_grade()=f()
2015-04-16
class Person(object):
count=0
def __init__(self,name):
self.name=name
Person.count=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=Person.count+1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
2015-04-16