高级函数对象 lambda函数 python使用lambda来创建匿名函数。所谓匿名函数,就是不再使用像def语句如许标准的情势界说函数。 [code]lambda [arg1,[arg2,...argn]]:expression[/code]使用lambda函数的语法界说函数,必要注意以下两点:
界说匿名函数 [code]sayHello = lambda : print("hello,python") sayHello()[/code]使用lambda函数实现两数相加 [code]sum = lambda arg1,arg2 : arg1+arg2; print('相加后的值:',sum(10,20)) print('相加后的值:',sum(20,20))[/code]map()函数 map()函数是python内置的高阶函数,会根据提供的函数对指定序列做映射,第一个参数function是个函数对序列中每个元素都调用function函数,返回包含每次function函数返回值的新序列 [code]map(function,iterable,...)[/code]
对列表中每个元素平方 [code]def fun(x): return x*x result=map(fun,[1,2,3]) print(list(result))[/code]对列表中每个元素加3 [code]result=map(lambda x: x+3,[1,3,5,6]) print(list(result))[/code]两个列表的每个元素实现相加 [code]result=map((lambda x,y:x+y),[1,2,3],[6,7,9]) print(list(result))[/code]reduce()函数 [code]reduce(function,iterable)[/code]
使用reduce()函数对列表中的元素举行累加 [code]from functools import reduce def add(x,y): return x+y data = [1,2,3,4] r=reduce(add,data) print(r)[/code] [code]from functools import reduce data=[1,2,3,4] fun=lambda x,y:x+y r2=reduce(fun,data) print(r2)[/code]该盘算相当于(((1+2)+3)+4)=10 迭代器 迭代器是访问聚集元素的一种方式。迭代器对象从聚集的第一个元素开始访问,直到全部元素被访问完结束。迭代器只能往前不能退却。 使用迭代器 [code]lst=[1,2,3,4] it=iter(lst) print(next(it)) print(next(it)) print(next(it))[/code]自界说迭代器 对于要返回迭代器的类,迭代器对象被要求支持下面两个魔法方法 (1)iter()返回迭代器自己。 (2)next()返回容器的下一个元素的值。 斐波那契数列 [code]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)[/code]自界说迭代器实现斐波那契数列 [code]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))[/code]生成器 生成器是迭代器的一种实现。生成器不会把效果生存在一个系列中,而是生存生成器的状态,在每次举行迭代时返回一个值,直到遇到StopIteration异常时结束。 生成器的好处是延迟盘算,一次返回一个效果。它不会一次生成全部的效果,这对于处置惩罚大数据量会非常有用。 生成器函数 在函数中如果出现了yield关键字,那么该函数就不再是平凡函数,而是生成器函数。 使用yield语句而不是return语句返回效果。yield语句一次返回一个效果,在每个效果中间挂起函数的状态,以便下次从它离开的地方继续执行。 简单的生成器函数 [code]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))[/code]使用生成器函数生成一个含有n个奇数的生成器,使用for语句迭代输出 [code]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)[/code]迭代器同样的效果 [code]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)[/code]使用生成器生成斐波那契数列 [code]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)[/code]装饰器 装饰器本质是一个python函数,它可以让其他函数在不必要做任何代码变更的条件下增长额外的功能,装饰器的返回值也是一个函数对象。 简单的装饰器 先界说两个简单的数学函数 [code]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)))[/code] [code]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)))[/code]使用装饰器传递参数 [code]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)))[/code]基于类的装饰器 [code]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()[/code]到此这篇关于Python语言中的紧张函数对象用法的文章就先容到这了,更多相关Python函数对象用法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章渴望各人以后多多支持脚本之家! 来源:https://www.jb51.net/python/328243ev7.htm 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
|手机版|小黑屋|梦想之都-俊月星空
( 粤ICP备18056059号 )|网站地图
GMT+8, 2025-7-1 19:39 , Processed in 0.033612 second(s), 20 queries .
Powered by Mxzdjyxk! X3.5
© 2001-2025 Discuz! Team.