def calc_prod(lst):
def lazy_sum():
return reduce(lambda x,y:x*y,lst)
return lazy_sum
print calc_prod([1, 2, 3, 4])()
def lazy_sum():
return reduce(lambda x,y:x*y,lst)
return lazy_sum
print calc_prod([1, 2, 3, 4])()
2017-12-22
super方法的好处是不用输入父类名,当父类名改变或继承关系改变时(比如class A(B) --> class A(C)),只需更改类的第一行,而不用改变类的内部
2017-12-21
def performance(unit):
def per_deco(func):
def wrapper(n):
startTime = time.time()
func(n)
endTime = time.time()
msecs = (endTime - startTime) * 1000
print "%f %s" %(msecs,unit)
return func(n)
return wrapper
return per_deco
这个相当于之前的装饰器又加了一个外壳
def per_deco(func):
def wrapper(n):
startTime = time.time()
func(n)
endTime = time.time()
msecs = (endTime - startTime) * 1000
print "%f %s" %(msecs,unit)
return func(n)
return wrapper
return per_deco
这个相当于之前的装饰器又加了一个外壳
2017-12-21
优化一下, 不要c
def __str__(self):
a, b = self.p, self.q
if a < b:
a, b = b, a
while b != 0:
a, b = b, a%b
return '%s/%s'%(self.p/a, self.q/a)
def __str__(self):
a, b = self.p, self.q
if a < b:
a, b = b, a
while b != 0:
a, b = b, a%b
return '%s/%s'%(self.p/a, self.q/a)
2017-12-21
def __str__(self):
a, b = self.p, self.q
if a < b:
a, b = b, a
c = 0
while b != 0:
c = b
a, b = b, a%b
if c == 0:
return '%s/%s'%(self.p, self.q)
return '%s/%s'%(self.p/c, self.q/c)
a, b = self.p, self.q
if a < b:
a, b = b, a
c = 0
while b != 0:
c = b
a, b = b, a%b
if c == 0:
return '%s/%s'%(self.p, self.q)
return '%s/%s'%(self.p/c, self.q/c)
2017-12-21
def cmp_ignore_case(s1, s2):
if s1.lower()>s2.lower():
return 1
if s1.lower()<s2.lower():
return -1
转化大小写只作用在比较阶段,并不影响输出结果的大小写
if s1.lower()>s2.lower():
return 1
if s1.lower()<s2.lower():
return -1
转化大小写只作用在比较阶段,并不影响输出结果的大小写
2017-12-21
def format_name(s):
return s[:1].upper() + s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[:1].upper() + s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2017-12-21