import time
def performance(unit):
def decorator(f):
def dec1(n):
print 'call %s() in %s %s' %(f.__name__,time.asctime(),unit)
return f(n)
return dec1
return decorator
def performance(unit):
def decorator(f):
def dec1(n):
print 'call %s() in %s %s' %(f.__name__,time.asctime(),unit)
return f(n)
return dec1
return decorator
2015-05-31
最赞回答 / lancelotzx
我理解你的问题为:为啥输出了4次?两次@performance 相当于performance(performance(factorial(10)))考虑performance(factorial(10)):取time1,执行f即factorial三次,取time2,打印输出时间差值;考虑 performance(performance(factorial(10))) :取time3,执行performance(factorial(10))三次,此时每执行一次则打印一次,取time4,打印输出时间差值;从...
2015-05-30
class Fib(object):
__fib = [0,1]
def __init__(self):
pass
def __call__(self,num):
while len(Fib.__fib) < num:
Fib.__fib.append(sum(Fib.__fib[-2:]))
return str(Fib.__fib[0:num])
__fib = [0,1]
def __init__(self):
pass
def __call__(self,num):
while len(Fib.__fib) < num:
Fib.__fib.append(sum(Fib.__fib[-2:]))
return str(Fib.__fib[0:num])
2015-05-28
__fib = [0,1,1,2,3]
def __init__(self, num):
self.num = num
while len(Fib.__fib)<num:
Fib.__fib.append(sum(Fib.__fib[-2:]))
def __str__(self):
return str([Fib.__fib[i] for i in range(0, self.num)])
def __len__(self):
return self.num
def __init__(self, num):
self.num = num
while len(Fib.__fib)<num:
Fib.__fib.append(sum(Fib.__fib[-2:]))
def __str__(self):
return str([Fib.__fib[i] for i in range(0, self.num)])
def __len__(self):
return self.num
2015-05-28
@score.getter
def grade(self):
if self.score < 60:
return "C"
elif self.score > 80:
return "A"
else:
return "B"
def grade(self):
if self.score < 60:
return "C"
elif self.score > 80:
return "A"
else:
return "B"
2015-05-27
class Fib(object):
lst = [0]
def __init__(self, num):
a,b = 0,1
while num-1:
a,b = b,a+b
self.lst.append(a)
num -= 1
print self.lst
def __len__(self):
return len(self.lst)
f = Fib(10)
print f
print len(f)
lst = [0]
def __init__(self, num):
a,b = 0,1
while num-1:
a,b = b,a+b
self.lst.append(a)
num -= 1
print self.lst
def __len__(self):
return len(self.lst)
f = Fib(10)
print f
print len(f)
2015-05-27
class Person(object):
def __init__(self, name, gender, birth, **kw):
self.name = name
self.gender = gender
self.birth = birth
for key, value in kw.items():
setattr(self,key,value)
...
def __init__(self, name, gender, birth, **kw):
self.name = name
self.gender = gender
self.birth = birth
for key, value in kw.items():
setattr(self,key,value)
...
2015-05-27
最赞回答 / 筱兵潇将
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于JavaScript的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成。编码:把一个Python对象编码转换成Json字符串 json.dumps()解码:把Json格式字符串解码转换成Py...
2015-05-27