def calc_prod(lst):
def mult(x, y):
return x * y
def prod():
return reduce(mult, lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def mult(x, y):
return x * y
def prod():
return reduce(mult, lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-03-03
顺便复习下reduce()函数hh
def calc_prod(lst):
def prod(x,y):
return x*y
def multip():
print reduce(prod,lst)
return multip
f = calc_prod([1, 2, 3, 4])
print f()
def calc_prod(lst):
def prod(x,y):
return x*y
def multip():
print reduce(prod,lst)
return multip
f = calc_prod([1, 2, 3, 4])
print f()
2018-03-03
import math
def add(x, y, f):
return f(x) + f(y)
def f(z):
return math.sqrt(z)
print add(25, 9,f)
def add(x, y, f):
return f(x) + f(y)
def f(z):
return math.sqrt(z)
print add(25, 9,f)
2018-03-02
sort()是默认是数字值,字符串ASCII值从小到大的排序,即sort([......],1)
字符一个一个比较,出现不同则停止比较
reseversed_cmp取值是前面的List,感到懵逼多半是这里记不住,理解时很伤啊....
字符一个一个比较,出现不同则停止比较
reseversed_cmp取值是前面的List,感到懵逼多半是这里记不住,理解时很伤啊....
2018-02-28
class Person(object):
count = 0
def __init__(self, name):
self.name = name
Person.count = Person.count + 1
count = 0
def __init__(self, name):
self.name = name
Person.count = Person.count + 1
2018-02-28
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)
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
。。。
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)
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
。。。
2018-02-28
def fib(self,num):
L = []
n,a,b = 0,0,1
while n < num-1:
if n == 0:
L.append(a)
L.append(b)
a,b = b,a+b
n += 1
return L
def __call__(self,x):
return str(self.fib(x))
L = []
n,a,b = 0,0,1
while n < num-1:
if n == 0:
L.append(a)
L.append(b)
a,b = b,a+b
n += 1
return L
def __call__(self,x):
return str(self.fib(x))
2018-02-27
class Student(Person):
__slots__ = ('score')
def __init__(self,name,gender,score):
Person.__init__(self,name,gender)
# super(Student, self).__init__(name, gender)
self.score = score
__slots__ = ('score')
def __init__(self,name,gender,score):
Person.__init__(self,name,gender)
# super(Student, self).__init__(name, gender)
self.score = score
2018-02-27
#创造属性
@property
def grade(self):
if self.score >= 80:
return 'A'
elif self.score >= 60:
return 'B'
return 'C'
@property
def grade(self):
if self.score >= 80:
return 'A'
elif self.score >= 60:
return 'B'
return 'C'
2018-02-27