def calc_prod(lst):
def helloworld():
return reduce(lambda x,y:x*y,(lst))
return helloworld
f = calc_prod([1, 2, 3, 4])
print f()
def helloworld():
return reduce(lambda x,y:x*y,(lst))
return helloworld
f = calc_prod([1, 2, 3, 4])
print f()
2017-12-16
class Person(object):
def __init__(self, name, score):
self.name=name
self.__score=score
p = Person('Bob', 59)
print p.name
try:
print p.__score
except AttributeError:
print 'attributeError'
def __init__(self, name, score):
self.name=name
self.__score=score
p = Person('Bob', 59)
print p.name
try:
print p.__score
except AttributeError:
print 'attributeError'
2017-12-16
python 3.x 中得到的是map类,想要与python 2.x 得到的结果一样只需用list()包含map进行输出,list(map(f,[1,2,3]))
2017-12-15
其实讲解的都很详细,其实就是因为当函数执行完后i的值不再从一开始迭代,如果还是不理解为什么,可以加一句话,如下
def count():
fs = []
i=1
for i in range(1, 4):
def f(m=i):
return m*m
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
def count():
fs = []
i=1
for i in range(1, 4):
def f(m=i):
return m*m
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2017-12-15
class Fib(object):
def __init__(self,num):
a,b,l=0,1,[]
for n in range(num):
l.append(a)
a,b=b,a+b
def __call__(self):
return str(l)
f = Fib()
print f(10)
没想到最后一节还是没写对
def __init__(self,num):
a,b,l=0,1,[]
for n in range(num):
l.append(a)
a,b=b,a+b
def __call__(self):
return str(l)
f = Fib()
print f(10)
没想到最后一节还是没写对
2017-12-15
print filter(lambda x:x and len(x.strip())>0, ['test', None, '', 'str', ' ', 'END'])
真的不智能
真的不智能
2017-12-15
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count + 1
@classmethod
def how_many(cls):
return cls.__count
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count + 1
@classmethod
def how_many(cls):
return cls.__count
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
2017-12-15
__str__ 魔术方法是用来返回字符串的 self.numbers是个数组,所以这里加str(self.numbers),不可以直接self.numbers
2017-12-15