| 
                            
                                  高级函数对象 lambda函数 python使用lambda来创建匿名函数。所谓匿名函数,就是不再使用像def语句这样标准的形式定义函数。 
	
		
			| 1 | lambda [arg1,[arg2,...argn]]:expression |  使用lambda函数的语法定义函数,需要注意以下两点: 
	冒号之前的arg1,arg2,…表示他们是这个函数的参数匿名函数不需要return来返回值,表达式本身的结果就是返回值 定义匿名函数 
	
		
			| 1 2 | sayHello = lambda : print("hello,python") sayHello() |  使用lambda函数实现两数相加 
	
		
			| 1 2 3 | sum = lambda arg1,arg2 : arg1+arg2; print('相加后的值:',sum(10,20)) print('相加后的值:',sum(20,20)) |  map()函数 map()函数是python内置的高阶函数,会根据提供的函数对指定序列做映射,第一个参数function是个函数对序列中每个元素都调用function函数,返回包含每次function函数返回值的新序列 
	
		
			| 1 | map(function,iterable,...) |  
	function: 是个函数,通过函数依次作用在序列中的每个元素中。iterable: 一个或多个序列,也被称为可迭代对象。 对列表中每个元素平方 
	
		
			| 1 2 3 4 | def fun(x):     return x*x result=map(fun,[1,2,3]) print(list(result)) |  对列表中每个元素加3 
	
		
			| 1 2 | result=map(lambda x: x+3,[1,3,5,6]) print(list(result)) |  两个列表的每个元素实现相加 
	
		
			| 1 2 | result=map((lambda x,y:x+y),[1,2,3],[6,7,9]) print(list(result)) |  reduce()函数 
	
		
			| 1 | reduce(function,iterable) |  
	function: 接收函数,必须有两个参数iterable: 一个或多个序列,序列也称为迭代对象 使用reduce()函数对列表中的元素进行累加 
	
		
			| 1 2 3 4 5 6 | from functools import reduce def add(x,y):     return x+y data = [1,2,3,4] r=reduce(add,data) print(r) |  
	
		
			| 1 2 3 4 5 | from functools import reduce data=[1,2,3,4] fun=lambda x,y:x+y r2=reduce(fun,data) print(r2) |  该计算相当于(((1+2)+3)+4)=10 迭代器 迭代器是访问集合元素的一种方式。迭代器对象从集合的第一个元素开始访问,直到所有元素被访问完结束。迭代器只能往前不能后退。 使用迭代器 
	
		
			| 1 2 3 4 5 | lst=[1,2,3,4] it=iter(lst) print(next(it)) print(next(it)) print(next(it)) |  自定义迭代器 对于要返回迭代器的类,迭代器对象被要求支持下面两个魔法方法 (1)iter()返回迭代器本身。 (2)next()返回容器的下一个元素的值。 斐波那契数列 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 13 | def fib(max):     a,b=1,1     idx=0     ls=[]     while True:         if idx==max:             return ls         ls.append(a)         a,b=b,a+b         idx=idx+1 if __name__=="__main__":     result = fib(5)     print(result) |  自定义迭代器实现斐波那契数列 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Fibs(object):     def __init__(self,max):         self.max=max         self.a=0         self.b=1         self.idx=0     def __iter__(self):         return self     def __next__(self):         fib=self.a         if self.idx==self.max:             raise StopIteration         self.idx=self.idx+1         self.a,self.b=self.b,self.a+self.b         return fib if __name__=='__main__':     fibs=Fibs(5)     print(list(fibs)) |  生成器 生成器是迭代器的一种实现。生成器不会把结果保存在一个系列中,而是保存生成器的状态,在每次进行迭代时返回一个值,直到遇到StopIteration异常时结束。 生成器的好处是延迟计算,一次返回一个结果。它不会一次生成所有的结果,这对于处理大数据量会非常有用。 生成器函数 在函数中如果出现了yield关键字,那么该函数就不再是普通函数,而是生成器函数。 使用yield语句而不是return语句返回结果。yield语句一次返回一个结果,在每个结果中间挂起函数的状态,以便下次从它离开的地方继续执行。 简单的生成器函数 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 | def mygen():     print('gen() 1')     yield 1     print('gen() 2')     yield 2     print('gen() 3')     yield 3 gen =mygen() print(next(gen)) print(next(gen)) print(next(gen)) |  使用生成器函数生成一个含有n个奇数的生成器,使用for语句迭代输出 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 | def odd(max):     n=1     count = 0     while True:         yield n         n+=2         count=count+1         if count == max:             raise StopIteration odd_num=odd(3) for num in odd_num:     print(num) |  迭代器同样的效果 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class odd(object):     def __init__(self,max):         self.count=0         self.max=max         self.start=-1     def __iter__(self):         return self     def __next__(self):         self.start+=2         self.count=self.count+1         if self.count==self.max:             raise StopIteration         return self.start odd_num=odd(3) for num in odd_num:     print(num) |  使用生成器生成斐波那契数列 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 13 | def Fibs(max):     a,b=1,1     count=0     while True:         if count == max:             raise StopIteration         yield a         a,b=b,a+b         count=count+1 if __name__=="__main__":     fib=Fibs(5)     for item in fib:         print(item) |  装饰器 装饰器本质是一个python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外的功能,装饰器的返回值也是一个函数对象。 简单的装饰器 先定义两个简单的数学函数 
	
		
			| 1 2 3 4 5 6 7 8 | def add(a,b):     return a+b def subtraction(a,b):     return a-b a=5 b=1 print('{0}+{1}={2}'.format(a,b,add(a,b))) print('{0}-{1}={2}'.format(a,b,subtraction(a,b))) |  
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | def decorator(func):     def inner(a,b):         print('输入参数 a={0},b={1}'.format(a,b))         f1=func(a,b)         return f1     return inner @decorator def add(a,b):     return a+b @decorator def subtraction(a,b):     return a-b a=5 b=1 print('{0}+{1}={2}'.format(a,b,add(a,b))) print('{0}-{1}={2}'.format(a,b,subtraction(a,b))) |  使用装饰器传递参数 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | def pre_str(pre=''):     def decorator(func):         def inner(a,b):             print('装饰器参数 pre={0}'.format(pre))             print('输入参数 a={0},b={1}'.format(a,b))             f1=func(a,b)             return f1         return inner     return decorator @pre_str('add') def add(a,b):     return a+b @pre_str('subtraction') def subtraction(a,b):     return a-b a=5 b=1 print('{0}+{1}={2}'.format(a,b,add(a,b))) print('{0}-{1}={2}'.format(a,b,subtraction(a,b))) |  基于类的装饰器 
	
		
			| 1 2 3 4 5 6 7 8 9 10 | class Test():     def __init__(self,func):         self.func=func     def __call__(self, *args, **kwargs):         print('The current function:%s' % self.func.__name__)         return self.func @Test def test1():     print("Hello") test1() |    
 |