def __init__(self, name, gender, **kw):
for k in kw:
setattr(self,k,kw[k])
self.name=name
self.gender=gender
for k in kw:
setattr(self,k,kw[k])
self.name=name
self.gender=gender
2018-08-28
def format_name(s):
head = s[0:1].upper()
tail = s[1:].lower()
return '%s%s' % (head,tail)
print map(format_name, ['adam', 'LISA', 'barT'])
head = s[0:1].upper()
tail = s[1:].lower()
return '%s%s' % (head,tail)
print map(format_name, ['adam', 'LISA', 'barT'])
2018-08-27
使用reduce函数要先从functools库中导入:from functools import reduce
2018-08-27
闭包中闭包,厉害,r = f(i)这句话是在调用这个函数吧之前在循环中,根本没有掉用过函数,只是将函数作为参数传回,所以,才会错的吧
2018-08-26
class Fib(object):
def __call__(self,num):
lists = [0,1]
for i in range(2,num):
lists.append(lists[i - 1] + lists[i - 2])
return lists
f = Fib()
print f(10)
def __call__(self,num):
lists = [0,1]
for i in range(2,num):
lists.append(lists[i - 1] + lists[i - 2])
return lists
f = Fib()
print f(10)
2018-08-26
def calc_prod(lst):
def aa(x,y):
return x*y
def a():
return reduce(aa,lst)
return a
f = calc_prod([1, 2, 3, 4])
print f()
def aa(x,y):
return x*y
def a():
return reduce(aa,lst)
return a
f = calc_prod([1, 2, 3, 4])
print f()
2018-08-23
终于看懂意思了,
例子里面没有用闭包的时候count()=[f(),f(),f()],f()的值就是9,
用了闭包以后count()=[f(1),f(2),f(3)],
答案里面用r来接收f(i)反而不直观,写成fs.append(f(i))就非常清楚了
例子里面没有用闭包的时候count()=[f(),f(),f()],f()的值就是9,
用了闭包以后count()=[f(1),f(2),f(3)],
答案里面用r来接收f(i)反而不直观,写成fs.append(f(i))就非常清楚了
2018-08-23
def count():
fs = []
for i in range(1, 4):
def f(i):
def j():
return i*i
return j
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(i):
def j():
return i*i
return j
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2018-08-22
class Person(object):
def __init__(self, name, gender, **kw):
self.name = name
self.gender=gender
for key,value in kw.items():
setattr(self,key,value)
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
def __init__(self, name, gender, **kw):
self.name = name
self.gender=gender
for key,value in kw.items():
setattr(self,key,value)
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
2018-08-20