@property
def grade(self):
if self.__score >= 80:
return 'A'
elif self.__score >= 60:
return 'B'
else:
return 'C'
def grade(self):
if self.__score >= 80:
return 'A'
elif self.__score >= 60:
return 'B'
else:
return 'C'
2018-01-28
偏函数的详解大家可以参考这里:
http://blog.csdn.net/appleyk/article/details/77609114
http://blog.csdn.net/appleyk/article/details/77609114
2018-01-28
def gcd(self, x, y):
if y > 0:
return self.gcd(y, x%y)
else:
return x
def __str__(self):
return '%s/%s' % (self.p/self.gcd(self.p, self.q), self.q/self.gcd(self.p, self.q))
if y > 0:
return self.gcd(y, x%y)
else:
return x
def __str__(self):
return '%s/%s' % (self.p/self.gcd(self.p, self.q), self.q/self.gcd(self.p, self.q))
2018-01-28
def calc_prod(lst):
#def lazy_prod():
def f(x, y):
return x * y
return reduce(f, lst, 1)
#return lazy_prod
f = calc_prod([1, 2, 3, 4])
print (f)
其实中间的lazy_prod可以省略,这样的话f就赋值一个数值而不是一个函数;如果不省略的话f其实被赋的函数是lazy_prod而并不是f()//很多人可能像我一样以为是f(),后来我就想如果是f()那不就直接可以得值,后来调试发现问题的
#def lazy_prod():
def f(x, y):
return x * y
return reduce(f, lst, 1)
#return lazy_prod
f = calc_prod([1, 2, 3, 4])
print (f)
其实中间的lazy_prod可以省略,这样的话f就赋值一个数值而不是一个函数;如果不省略的话f其实被赋的函数是lazy_prod而并不是f()//很多人可能像我一样以为是f(),后来我就想如果是f()那不就直接可以得值,后来调试发现问题的
2018-01-28
Python NameError: name 'reduce' is not defined
今天在搜用Python求阶乘的时候, 搜出来的最简单的是用reduce这个built-in function, 但是我在用reduce的时候, 却报NameError: name 'reduce' is not defined. 于是又搜了一下,发现在python 3.0.0.0以后, reduce已经不在built-in function里了, 要用它就得from functools import reduce.
今天在搜用Python求阶乘的时候, 搜出来的最简单的是用reduce这个built-in function, 但是我在用reduce的时候, 却报NameError: name 'reduce' is not defined. 于是又搜了一下,发现在python 3.0.0.0以后, reduce已经不在built-in function里了, 要用它就得from functools import reduce.
2018-01-28
def __init__(self, num):
L = [0,1]
for x in range(num-2):
L.append(L[-1]+L[-2])
self.names = L
#帅
def __init__(self, num):
a, b, L = 0, 1, []
for i in range(num):
L.append(a)
a, b = b, a+b
self.names = L
L = [0,1]
for x in range(num-2):
L.append(L[-1]+L[-2])
self.names = L
#帅
def __init__(self, num):
a, b, L = 0, 1, []
for i in range(num):
L.append(a)
a, b = b, a+b
self.names = L
2018-01-28
这个方法简直帅爆了:
def __init__(self, num):
L = [0,1]
for x in range(num-2):
L.append(L[-1]+L[-2])
self.names = L
def __str__(self):
return '%s' % self.names
def __len__(self):
return len(self.names)
def __init__(self, num):
L = [0,1]
for x in range(num-2):
L.append(L[-1]+L[-2])
self.names = L
def __str__(self):
return '%s' % self.names
def __len__(self):
return len(self.names)
2018-01-27
def __init__(self, num):
for x in range(num):
self.b.append(self.F(x))
self.names = self.b
def __str__(self):
return '%s'% self.b
def __len__(self):
return len(self.names)
for x in range(num):
self.b.append(self.F(x))
self.names = self.b
def __str__(self):
return '%s'% self.b
def __len__(self):
return len(self.names)
2018-01-27
def F(self, x):
if x == 0:
return 0
if x == 1:
return 1
return self.F(x-1)+self.F(x-2)
if x == 0:
return 0
if x == 1:
return 1
return self.F(x-1)+self.F(x-2)
2018-01-27
if self.score < s.score:
return 1
elif self.score > s.score:
return -1
else:
if self.name < s.name:
return -1
elif self.name > s.name:
return 1
else:
return 0
return 1
elif self.score > s.score:
return -1
else:
if self.name < s.name:
return -1
elif self.name > s.name:
return 1
else:
return 0
2018-01-27
def __str__(self):
return '(Student: %s, %s, %s)' % (self.name, self.gender, self.score)
__repr__ = __str__
return '(Student: %s, %s, %s)' % (self.name, self.gender, self.score)
__repr__ = __str__
2018-01-27