try:
print Person.__count
except AttributeError:
print 'AttributeError'
print Person.__count
except AttributeError:
print 'AttributeError'
2018-03-08
def format_name(s):
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-03-08
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-03-08
递归函数法:
class Fib(object):
省略初始化部分
self.L=[]
for x in range(num):
def fn(x):
if x==0:
return 0
elif x==1:
return 1
return fn(x-2)+fn(x-1)
self.L.append(fn(x))
def __str__(self):
return '%s'%self.L
def __len__(self):
return len(self.L)
class Fib(object):
省略初始化部分
self.L=[]
for x in range(num):
def fn(x):
if x==0:
return 0
elif x==1:
return 1
return fn(x-2)+fn(x-1)
self.L.append(fn(x))
def __str__(self):
return '%s'%self.L
def __len__(self):
return len(self.L)
2018-03-08
最赞回答 / 慕姐1978998
import functools
sorted_ignore_case = functools.partial(sorted,cmp=lambda w1, w2: -cmp(w1.upper(),w2.upper()))
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])试试这个,完美执行
2018-03-08
def realprod(a,b):
return a * b
def calc_prod(lst):
def prod():
return reduce(realprod,lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
return a * b
def calc_prod(lst):
def prod():
return reduce(realprod,lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-03-07
import time
def performance(f):
def fn(*args, **kw):
print 'call' + f.__name__ + '() in,' ,time.time()
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):
print 'call' + f.__name__ + '() in,' ,time.time()
return f(*args, **kw)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2018-03-07
class BStudent(Student,BasketballMixin):
def __init__(self):
super(BStudent,self).__init__()
class FTeacher(Teacher,FootballMixin):
def __init__(self):
super(FTeacher,self).__init__()
s = BStudent()
print s.skill()
t = FTeacher()
print t.skill()
def __init__(self):
super(BStudent,self).__init__()
class FTeacher(Teacher,FootballMixin):
def __init__(self):
super(FTeacher,self).__init__()
s = BStudent()
print s.skill()
t = FTeacher()
print t.skill()
2018-03-07