迭代器
每一个可迭代类内部都要实现__iter__()方法,返回一个迭代类对象,迭代类对象则定义了这个可迭代类如何迭代。
for循环调用list本质上是是调用了list的迭代器进行迭代。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# 对list进行for循环本质上是调用了list的迭代器 list = [ 1 , 2 , 3 , 4 ] # for 循环调用 for elem in list : print (elem) # 迭代器调用 list_iter = list .__iter__() while True : try : print ( next (list_iter)) except StopIteration: break |
实现一个自己自定的迭代类,规定迭代一个可迭代的数据结构为“倒计时”模式。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# 可迭代对象类 class CountDown( object ): def __init__( self ,num): self .num = num def __iter__( self ): return MyIterator( self .num) # 迭代类 class MyIterator( object ): def __init__( self ,num): self .NUM = num self .FINAL = 0 self .now = num def __iter__( self ): return self def __next__( self ): step = 1 if self .NUM< self .FINAL else - 1 while self .now ! = self .FINAL: self .now + = step return self .now - step raise StopIteration cd_pos = CountDown( 5 ) cd_neg = CountDown( - 5 ) for i,j in zip (cd_pos,cd_neg): print (f 'pos:{i}\tneg:{j}' ) |
生成器
含有yield指令的函数可以称为生成器,它可以将函数执行对象转化为可迭代的对象。这样就可以像debug一样一步一步推进函数。可以实现的功能是可以实现让函数内部暂停,实现了程序的异步功能,这样可以及进行该函数与外部构件的信息交互,实现了系统的解耦。
1
2
3
4
5
6
7
8
|
from collections import Iterable def f(): pass # 含有yield指令的函数可以称为生成器 def g(): yield () print ( type (f()), isinstance (f(),Iterable)) print ( type (g()), isinstance (g(),Iterable)) |
使用生成器可以降低系统的耦合性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
import os # 生成器是迭代器的一种,让函数对象内部进行迭代 # 可以实现让函数内部暂停,实现了程序的异步功能,同时也实现了解耦。 def my_input(): global str str = input ( 'input a line' ) pass def my_write(): with open ( 'workfile.txt' , 'w' ) as f: while ( str ): f.write( str + '\n' ) yield () return mw = my_write() while ( True ): my_input() try : next (mw) except StopIteration: pass if not str : break |
装饰器
装饰器封装一个函数,并且用这样或者那样的方式来修改它的行为。
不带参数的装饰器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# 不带参数的装饰器 from functools import wraps # 装饰器封装一个函数,并且用这样或者那样的方式来修改它的行为。 def mydecorator(a_func): @wraps (a_func) #声明这个注解就可以不重写传入的函数,只是调用的时候wrap一下。不加的话,a_func函数可以看作被重写为wrapTheFunction. def wrapTheFunction(): print (f "function in {id(a_func)} starts..." ) a_func() print (f "function in {id(a_func)} ends..." ) return wrapTheFunction # 在函数定义前加入此注解就可以将函数传入装饰器并包装 @mydecorator def f(): print ( 'hi' ) pass f() print (f.__name__) |
带参数的装饰器(实现输出到自定义的日志文件)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# 带参数的装饰器(实现输出到自定义的日志文件) from functools import wraps def logit(logfile = 'out.log' ): def mydecorator2(a_func): @wraps (a_func) def wrapTheFunction( * args, * * kwargs): # 这个保证了函数可以含有任意形参 log_string = a_func.__name__ + " was called" print (log_string) # 打开logfile,并写入内容 with open (logfile, 'a' ) as opened_file: # 现在将日志打到指定的logfile opened_file.write(log_string + '\n' ) return a_func( * args, * * kwargs) return wrapTheFunction return mydecorator2 # func group1 @ logit( 'out1.log' ) def func1( str ): print ( str ) pass @ logit( 'out2.log' ) def func2(): pass func1( 'I have a foul smell' ) func2() |
实现一个装饰器类(这样写可以简化装饰器函数,并且提高封装性)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# 带参数的装饰器(实现输出到自定义的日志文件) from functools import wraps def logit(logfile = 'out.log' ): def mydecorator2(a_func): @wraps (a_func) def wrapTheFunction( * args, * * kwargs): # 这个保证了函数可以含有任意形参 log_string = a_func.__name__ + " was called" print (log_string) # 打开logfile,并写入内容 with open (logfile, 'a' ) as opened_file: # 现在将日志打到指定的logfile opened_file.write(log_string + '\n' ) return a_func( * args, * * kwargs) return wrapTheFunction return mydecorator2 # func group1 @ logit( 'out1.log' ) def func1( str ): print ( str ) pass @ logit( 'out2.log' ) def func2(): pass func1( 'I have a foul smell' ) func2() |
总结
到此这篇关于python中的迭代器,生成器与装饰器详解的文章就介绍到这了,更多相关python迭代器,生成器与装饰器内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/FangNJ/article/details/122986008