Python的迭代器(iterator)和生成器(constructor)

一、迭代器(iterator)

1.迭代器的概述

在Python中,for循环可以用于Python中的任何类型,包括列表、元祖等等,实际上,for循环可用于任何“可迭代对象”,这其实就是迭代器

迭代器是一个实现了迭代器协议的对象,Python中的迭代器协议就是有next方法的对象会前进到下一结果,而在一系列结果的末尾是,则会引发 StopIteration。任何这类的对象在Python中都可以用for循环或其他遍历工具迭代,迭代工具内部会在每次迭代时调用next方法,并且 捕捉StopIteration异常来确定何时离开。

迭代器的另一个优点就是它不要求你事先准备好整个迭代过程中所有的元素。迭代器仅仅在迭代至某个元素时才计算该元素,而在这之前或之后,元素可以不 存在或者被销毁。这个特点使得它特别适合用于遍历一些巨大的或是无限的集合,比如几个G的文件,或是斐波那契数列等等。这个特点被称为延迟计算或惰性求值 (Lazy evaluation)。

迭代器更大的功劳是提供了一个统一的访问集合的接口。只要是实现了__iter__()方法的对象,就可以使用迭代器进行访问。

2.迭代器的使用

使用内建的工厂函数iter(iterable)可以获取迭代器对象:注意:迭代器是对象

 lst = range(2)
it = iter(lst)
print it
#输出结果
<listiterator object at 0x00BB62F0>

使用迭代器的next()方法可以访问下一个元素:

 print it.next()
0

如何判断迭代器还有更多的元素可以访问呢?Python里的迭代器并没有提供类似has_next()这样的方法。
那么在这个例子中,我们如果访问到了最后一个元素1后,再使用next()方法会怎样呢?

it.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration

Python遇到这样的情况时将会抛出StopIteration异常。事实上,Python正是根据是否检查到这个异常来决定是否停止迭代的。
这种做法与迭代前手动检查是否越界相比各有优点。但Python的做法总有一些利用异常进行流程控制的嫌疑。

看到这个地方大家应该明白了迭代器的原理了吧,现在来说一说前面所说到的列表,元祖等可以迭代的原理了,还是要归功于自带的__iter__(前面和后面加上‘__’就是内置方法哦)

实例:

 list1 = range(5) #生成一个list
print '*****list1=', list1 #打印list1内容
print '*****type=', type(list1) #打印list类型
it = iter(list1) #讲list1变成迭代器
print '*****type(iter)=', type(it) #打印生成后的对象类型
print '*****it=', it #打印it的信息
print '*****dir(list1)=', dir(list1) #查看列表方法

运行结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABU0AAADwCAIAAAC7aNIOAAAgAElEQVR4nO2dvW8st9m3848EsfUkXrVH1dMIboVVDGRV+lWVADLkwhICS6neMlAqAzpNFki5Lt0IqhYw5NaloOosnNrtAVQpeBHgLWZ2l+SQNzmfJGeuwVVIyxkOed8kh7/hx/zmVw4O2/GnP/1pdngAAAAAAAAAyfLb3/72008/PTg4+J//+Z/f//73f/jDHz777LPfxJaTHIke6HwAAAAAAIDEQedz1DjQ+QAAAAAAAImDzueocaDzAQAAAAAAEgedz1HjQOcDAAAAAAAkDjqfo8aBzgcAAAAAAEgcu84/4uDg4ODg4ODg4ODg4ODgGMvxm//LwcHBwcHBwcHBwcHBwcExluM3/4eDg4ODg4ODg4ODg4ODg2MsBzqfg4ODg4ODg4ODg4ODg2M8Bzqfg4ODg4ODg4ODg4ODg2M8x2+ibw8IAAAAAAAAAF2BzgcAAAAAAAAYD+h8AAAAAAAAgPGAzgcAAAAAAAAYD5LOv1m/flieJhN6vX57fduyvrVeW5zzslzEt2wYV2ebjxc7Vlex0wMmxyvFQZuH49jpAQAAAAAAkLHp/NvHQmYvli/FHzdrRVfHClWS14POP11uXt827xeuExbvP0jvFxpzdbb5eH43F0K3CvP5ZN7pref355uPF5uPZ5ddRnt099yLKt6mtuTp/qhLL5RsJb092ccrdD4AAAAAAGSARecvli9vb69vm/c3y5cPy+vl5vXtbT/AHit0j6TzGyPo/Ov1NlXD6vyrM2WE/3jVodS/Ott8vHi6P7577lbnH909K0p4fvLUkzAu09+x1L98kF9PoPMBAAAAACALnPP2b9bbSfLr63RCZ4cD6/zT5aa812JYnV9RlZrsb8H85KnU9kdd63yTy4cLabZCc47unrue4FCaVxDz6HwAAAAAAMiC0eh87+r902KCwJs+sb+cRGDh8UaPYVidX1H15Whzlzqzd51fTLbvYdMBfeJAB+w0PDofAAAAA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M6HwAAAAAAYFKg8+vkPWaONNkvMj95KrX9ETofAAAAAABgYnSu87WF/YbCPF59vHi6P1LP0YSrdO3s8KBQsNZ57FtFuj+hkkJ5Dry+H0ENnd8mv75o1ZMvHxqslnfrfKclzfSj8wEAAAAAALKit/H8ywe77lX1sO0c9+/lXPSj8t/5yWr3tzdmTTYf3T1r4rZYx74X1Y3G89vk12vk+f355uHESHYADp0vWbKSfnQ+AAAAAABATkTQ+S75LV8r39ca8+7kil6dnzzpS9+fVKHbsc7359dh5PLC+f15Ie8vH7rR+cErL9D5AAAAAAAA2dHNfvuWM526N2DNuTCe70iVGLPlLoq2r4Z2q/MbrbHf6fydyD/sTueLlhTTDwAAAAAAAKkz/Hh+Y51/ELA+3xqzufZe/zJ8NafJ6Pw7ReRXlxsEwPp8AAAAAACAqZGXztfSoJ5TdzxfzGkCOr98NyG/y/Dj32+/Ykkx/QAAAAAAAJA6eer8SjLkmCsr8FW0tfqzrTaOrfPtuwaY15Yq3TkgH/RdPbdD0fkAAAAAAADZkY/Ov3yoqPG9vvXEbO6or6FGdXT3fPH0cPYUX+cbabZK7t1+/q7U2nW+aEkx/QAAAAAAAJA6Xev88jPvBqWM9Ohe8drZVrXWX5+v/OLYO3AnmC9WV8UIf6jOb5NfP2qabXrbMZ6vG8oSg2BJFXQ+AAAAAABAdtTX+TAZ0PkAAAAAAADZgc4HJ+h8AAAAAACA7EDngxN0PgAAAAAAQHZIOn+/hLvGknLInv1uBa59AQAAAAAAACBVXDofAAAAAAAAAPIDnQ8AAAAAAAAwHtD5AAAAAAAAAOMBnQ8AAAAAAAAwHtD5AAAAAAAAAOMBnQ8AAAAAAAAwHtD5AAAAAAAAAOMBnQ8AAAAAAAAwHtD5AAAAAAAAAOMBnQ8AAAAAAAAwHv4/W+ELZfJAtVMAAAAASUVORK5CYII=" alt="" />

首先python对关键字in后的对象调用iter函数迭代器,然后调用迭代器的next方法获得元素,直到抛出StopIteration异常。

因为迭代器如此普遍,python专门为for关键字做了迭代器的语法糖。在for循环中,Python将自动调用工厂函数iter()获得迭代器,自动调用next()获取元素,还完成了检查StopIteration异常的工作。

二、生成器(Generator)

1. yield解析

yield 关键字用来定义生成器(Generator),其具体功能是可以当return使用,从函数里返回一个值,不同之处是用yield返回之后,可以让函数从 上回yield返回的地点继续执行。也就是说,yield返回函数,交给调用者一个返回值,然后再“瞬移”回去,让函数继续运行, 直到下一跳yield语句再返回一个新的值。

使用yield返回后,调用者实际得到的是一个迭代器对象,迭代器的值就是返回值,而调用该迭代器的next()方法会导致该函数恢复yield语句的执行环境继续往下跑,直到遇到下一个yield为止,如果遇不到yield,就会抛出异常表示迭代结束。

看一个例子:

def test_yield():
yield 1
yield 2
yield (1,2) test = test_yield()
print test.next()
print test.next()
print test.next()

运行结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZIAAAGYCAIAAADne/PLAAAf5klEQVR4nO3dPa/0zF3H8anukrwDWkRBi5BrXkSEqKegTMEpKaiRpqFIggigwBGXELoDihWk6EqKCIJISHLnhMbKCzivYii8D+N5nlnv7vyPvx9dxXXWa3vstX87M7Zn1S9/+b8//el//emffP2b3/ymBYDhKWILgCzEFgBhiC0AwhBbAIQhtgAIQ2wBEIbYAnCzWSs9ey9MZrnT2tRq+NiatVJqMotdzKSUt4fGcSlm/dt7NmX3vfG03RvZA437sLj4AQ6bdDFmrc7ihdtzb9zNrCNlXMwU36ibPpTTDsvVttbF+p6yD2d93sDr/wa0b2wlJ+++N562extiqyfjBzlsisVIb5uA2IqG1mVKfGP7PpRLEtbVthYzPXnXXaN76Nhq1Btbu++Np+3ehizqia1BDptiMXqr3QNIh5ZN1Lj6P5TLbpIXW88vy35uj62d9sbTdu/DYuuph02xGHJjq1TyyPTuD+U6Y12XfLj4MEad1F1745LNdmfCTp/VpjXrFjQ1IVnC7Haduxmv813fme2kCFvb63vWz/Q69byeaOv8eVmd2IuZvWGzn7I7yZ2a2IfFvbF9ww6HVPJ4ym9yv9jJX+j2SpYxtefzZ2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t7EDJufS5BHGiddkcW9WbTGwBt2m8b8sb3vP5ta3s0KDJ9Anyl9oWIEdTbAV9QKkBocKOnstcm5M21tGTiq2O8ZrzseV1gdVsl1vsYt9WfJOJLeBWFTdA9A15rBPtL++eUvf8zd9umllXarzmTKXJ/bGfyUTGuoquKyxhatMyOUVsAbd5+MM9nLQAbkNsARCG2AIgDCNAABBmiNhSSimlnrV2ALIQWwCEIbYACENsARCG2AIgDLEFQJhSbC3GqOlzdLCXvknRQhBbAKqVa1tvWr0o9Rq7RbRvUqQQxBaAalWNxHczvShlYmNy9k3yC0FsAahW3be1GKPUS/S5nL5JbiGILQDV2sbbelXqZTLvu006F4LYAlCN2hYAYepia9bJXqq+SV4hiC0A1biSCEAY7tsCIAx3yQMQhtgCIAyxBUAYYguAMEPEFgDUI7YACENsARCG2AIgDLEFQJj/B+TlwAkUBEOVAAAAAElFTkSuQmCC" alt="" />

2.yield的使用原理

1. 包含yield的函数

假如你看到某个函数包含了yield,这意味着这个函数已经是一个Generator,它的执行会和其他普通的函数有很多不同。比如下面的简单的函数:

 def h():
print 'To be'
yield 5
h()

可以看到,调用h()之后,print 语句并没有执行!这就是yield,那么,如何让print 语句执行呢?这就是后面要讨论的问题,通过后面的讨论和学习,就会明白yield的工作原理了。

参考文章:http://www.cnblogs.com/huxi/archive/2011/07/01/2095931.html

参考文章:http://blog.163.com/l_greatsea/blog/static/2049860442013220113640476/

上一篇:ASP.NET SignaiR 实现消息的即时推送,并使用Push.js实现通知


下一篇:与众不同 windows phone (9) - Push Notification(推送通知)之概述, 推送 Toast 通知