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
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
def calc_prod(lst):
def chenji(x,y):
return x*y
def chen():
return reduce(chenji,lst)
return chen
f = calc_prod([1, 2, 3, 4])
print f() 哈哈哈哈 竟然让我写出来了
def chenji(x,y):
return x*y
def chen():
return reduce(chenji,lst)
return chen
f = calc_prod([1, 2, 3, 4])
print f() 哈哈哈哈 竟然让我写出来了
2015-05-27
import math
def is_sqr(x):
return (math.sqrt(x)) in range(1,101)
print filter(is_sqr, range(1,101))
#我这个写法简直投机取巧...
def is_sqr(x):
return (math.sqrt(x)) in range(1,101)
print filter(is_sqr, range(1,101))
#我这个写法简直投机取巧...
2015-05-26
发现不能在类的初始化函数中定义通过'self.attr'来控制类属性而应该用‘类名.attr’来操作类属性
2015-05-26
self.name=name
self.gender=gender
for k,v in kw.iteritems():
setattr(self,k,v)
self.gender=gender
for k,v in kw.iteritems():
setattr(self,k,v)
2015-05-25
class Fib(object):
def __init__(self):
pass
def __call__(self, num):
f = [0, 1]
for i in range(num-2):
f.append(f[i] + f[i+1])
return f
f = Fib()
print f(10)
def __init__(self):
pass
def __call__(self, num):
f = [0, 1]
for i in range(num-2):
f.append(f[i] + f[i+1])
return f
f = Fib()
print f(10)
2015-05-24