def performance(f):
def fn(x):
start = time.time()
r = f(x)
print r
print "call " + f.__name__ + "() in:" + str(time.time() - start)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def fn(x):
start = time.time()
r = f(x)
print r
print "call " + f.__name__ + "() in:" + str(time.time() - start)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2015-12-22
def count():
fs = []
for i in range(1, 4):
def m(a = i):
return a*a
fs.append(m)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def m(a = i):
return a*a
fs.append(m)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-12-22
已采纳回答 / 清波
Object.method() 是个基本的概念。 就是 对象.方法名(), 所以 如果 某个对象 想 调用 某方法,就需要保证 这个对象有这个方法, None 是没有strip()方法的, 只有字符串有。 代码里面也没有在 lambda语...
2015-12-22
def cmp_ignore_case(s1, s2):
if s1[0].upper()>s2[0].upper():
return 1
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1[0].upper()>s2[0].upper():
return 1
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2015-12-21
def count():
fs = []
a = 1
b = 2
c = 3
def f1():
return a*a
def f2():
return b*b
def f3():
return c*c
fs.append(f1)
fs.append(f2)
fs.append(f3)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
haha ,不要笑。。。。。。。
fs = []
a = 1
b = 2
c = 3
def f1():
return a*a
def f2():
return b*b
def f3():
return c*c
fs.append(f1)
fs.append(f2)
fs.append(f3)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
haha ,不要笑。。。。。。。
2015-12-19
def cmp_ignore_case(s1, s2):
return cmp(s1.upper(),s2.upper())
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
return cmp(s1.upper(),s2.upper())
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2015-12-18
已采纳回答 / 努力提升
a, b = b, a + b 这种赋值,先计算右边的 那么 b=1, a+b=1,再赋值给a和b,那么 a=1, b=1而a = b,此时 b=1, 那么a=1, b = a+b, 那么 b=2所以结果是不同的
2015-12-18
如何去掉`__xxx__`这类的特殊属性,只保留我们自己定义的属性?
print filter(lambda x: x[0:2]!='__', dir(s))
print filter(lambda x: x[0:2]!='__', dir(s))
2015-12-17
class Student(Person):
__slots__ = ('name', 'gender', 'score')
def __init__(self, name, gender, score):
super(Student, self).__init__(name, gender)
self.score = score
__slots__ = ('name', 'gender', 'score')
def __init__(self, name, gender, score):
super(Student, self).__init__(name, gender)
self.score = score
2015-12-15