def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-04-09
import math
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
2018-04-09
def performance(f): 定义装饰
def fn(*args, **kw): 定义多参数闭包
t1 = time.time() 记下函数调用前时间
r = f(*args, **kw) 调用函数
t2 = time.time() 记下函数调用后时间
print 'call %s() in %fs' % (f.__name__, (t2 - t1)) print里两个参数,一个%s是字符串,接受f._name_,另一个%f是浮点数,接收两个时间的差(t2-t1)
def fn(*args, **kw): 定义多参数闭包
t1 = time.time() 记下函数调用前时间
r = f(*args, **kw) 调用函数
t2 = time.time() 记下函数调用后时间
print 'call %s() in %fs' % (f.__name__, (t2 - t1)) print里两个参数,一个%s是字符串,接受f._name_,另一个%f是浮点数,接收两个时间的差(t2-t1)
2018-04-09
def __init__(self, num):
x=0
y=1
l=0
z=1
f=[0]
while z<10:
f.append(y)
l=y
y=y+x
x=l
z+=1
self.num=f
x=0
y=1
l=0
z=1
f=[0]
while z<10:
f.append(y)
l=y
y=y+x
x=l
z+=1
self.num=f
2018-04-08
def __cmp__(self, s):
if self.name!=s.name:
return -cmp(self.score,s.score)
return cmp(self.name,s.name)
if self.name!=s.name:
return -cmp(self.score,s.score)
return cmp(self.name,s.name)
2018-04-08
-------*******----------
class Student(Person):
__slots__ = ('score')
def __init__(self,name,gender,score):
super(Student,self).__init__(name,gender)
self.name = name
self.gender = gender
self.score = score
class Student(Person):
__slots__ = ('score')
def __init__(self,name,gender,score):
super(Student,self).__init__(name,gender)
self.name = name
self.gender = gender
self.score = score
2018-04-07
def __cmp__(self, s):
if self.score>s.score:
return -1
elif self.score<s.score:
return 1
else:
return 1 if self.name>s.name else -1
L = [Student('Tim', 99), Student('Bob', 88), Student('Alice', 99)]
print sorted(L)
if self.score>s.score:
return -1
elif self.score<s.score:
return 1
else:
return 1 if self.name>s.name else -1
L = [Student('Tim', 99), Student('Bob', 88), Student('Alice', 99)]
print sorted(L)
2018-04-07