def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2015-09-07
最新回答 / 世间如玉
所谓“延迟执行”,是指返回的是一个函数而不是值,这个函数只有在调用的时候执行,也就是“延迟执行”了。fn函数在log返回时并没有执行,在调用factorial函数时(此时factorial和fn是一个函数),fn才被调用执行。
2015-09-07
import time
def performance(f):
def fn(*args,**kw):
t1=time.time()
t2=time.time()
print 'call '+f.__name__+'() in %fs' %(t2-t1)
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):
t1=time.time()
t2=time.time()
print 'call '+f.__name__+'() in %fs' %(t2-t1)
return f(*args,**kw)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2015-09-07
最赞回答 / 干涉
自己想了很久,才转过弯来,format_name()函数中s,不是['adam', 'LISA', 'barT']而是['adam', 'LISA', 'barT']中每个元素调用format_name()函数,就是format_name('adam')执行 s[0].upper() + s[1:].lower() 进行字符串转换
2015-09-07
class Fib(object):
def __init__(self, num):
m = []
for x in range(num):
if x == 0:
m.append(0)
elif x == 1:
m.append(1)
else:
m.append(m[-2]+m[-1])
self.o = m
def __init__(self, num):
m = []
for x in range(num):
if x == 0:
m.append(0)
elif x == 1:
m.append(1)
else:
m.append(m[-2]+m[-1])
self.o = m
2015-09-07
(2):
def count():
l = []
for x in range(1,4):
def f():
return x*x
l.append(f())
return l
print count()
def count():
l = []
for x in range(1,4):
def f():
return x*x
l.append(f())
return l
print count()
2015-09-06
你们运行下这两段代码
(1):
def count():
l = []
for x in range(1,4):
def f():
return x*x
l.append(f)
return l
print count()
print count()[0]()
print count()[1]()
print count()[2]()
(1):
def count():
l = []
for x in range(1,4):
def f():
return x*x
l.append(f)
return l
print count()
print count()[0]()
print count()[1]()
print count()[2]()
2015-09-06
难度跨越好大,不看答案做不出来,看了答案也不过似懂非懂,time模块挺突兀的,不提前知道都不知怎么冒出来的,__name__这个东西虽然猜也能猜到是啥,但是没有讲过终究是不能很好的理解。这节能再细化些就好了。。。
2015-09-06
# -*- coding:utf8 -*-
# 第一行记得写以上代码
class Person(object):
# ...
def get_grade(self):
if self.__score >= 90:
return u'A-优秀'
elif self.__score >= 60:
return u'B-及格'
else:
return u'C-不及格'
# 第一行记得写以上代码
class Person(object):
# ...
def get_grade(self):
if self.__score >= 90:
return u'A-优秀'
elif self.__score >= 60:
return u'B-及格'
else:
return u'C-不及格'
2015-09-06
class Person(object):
def __init__(self,name,gender,birth,**kw):
self.name = name
self.gender = gender
self.birth = birth
for k,v in kw.items():
setattr(self,k,v)
def __init__(self,name,gender,birth,**kw):
self.name = name
self.gender = gender
self.birth = birth
for k,v in kw.items():
setattr(self,k,v)
2015-09-06