面向對象編程之生成器與迭代器

生成器

列表推導式
[i * i for i in range(8)]
>
[0, 1, 4, 9, 16, 25, 36, 49]
生成器表達式
(i * i for i in range(8))
>
<generator object <genexpr> at 0x0000025C8F9019A8>

查看生成器對應的所有元素,有兩種方式:

1.多次調用內置函數next(),每次調用都返回生成器的下一個元素,直到拋出異常StopIteration時表示沒有更多元素了。
2.使用for-in語句對生成器進行迭代,這樣就不需要關心異常StopIteration了。

生成器中保存的並不是其對應的所有元素,而是如何推算出所有元素的算法。將生成器用於for-in語句時,元素是在循環的過程中不斷被推送出來的。將生成器作爲內置函數next()的實參時,返回的下一個元素也是在調用函數時被推送出來的。因此,生成器是惰性推算的,也就是說,只有當用到生成器中的某個元素時,纔會臨時進行推算,而並不會提前推算出來。
如果需要創建一個元素個數較大的容器,就可以考慮使用生成器,從而節約大量的存儲空間。

生成器函數 yield

生成器函數中通過關鍵字yield返回推算出的元素。生成器函數與普通函數的區別在於:當調用 內置函數next()或使用for-in語句進行迭代時,執行完yield語句就會將生成器函數掛起,下次會從掛起的地方繼續執行。
def fib(n):
    i = 0
    a,b = 1,1
    while i < n:
        print(a,end = ',')
        a,b = b,a+b
        i+=1
fib(6)
>
1,1,2,3,5,8,
def fib(n):
    i = 0
    a,b = 1,1
    while i < n:
        yield a
        a,b = b,a+b
        i+=1
fib(6)
>
<generator object fib at 0x0000021D6F86FB10>
gf = fib(6)
print(next(gf))
print(next(gf))
print(next(gf))
print(next(gf))
print(next(gf))
print(next(gf))
print(next(gf))
>
1
1
2
3
5
8
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-7-2e6b10837911> in <module>()
      6 print(next(gf))
      7 print(next(gf))
----> 8 print(next(gf))

StopIteration: 

gf = fib(6)
for item in gf:
    print(item)
>
1
1
2
3
5
8

遇到return生成器終止

只要有yield就是生成器

def test():
    n = 0
    while n < 10:
        yield n
        n+=2
        return '返回值作爲異常的提示語'
a = test()
print(a)
>
<generator object test at 0x000001A825EE9390>
next(a)
>
0
next(a)
>
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-11-3f6e2eea332d> in <module>()
----> 1 next(a)

StopIteration: 返回值作爲異常的提示語

迭代器

可以用於for-in語句的對象被稱爲可迭代(iterable)對象

例如:range,列表,元組,字符串,字典,集合,生成器都是可迭代對象

可以調用內置函數isinstance()判斷一個對象是否是可迭代對象。標準庫模塊collections中的類Iterable用於表示可迭代對象
from collections import Iterable
print(isinstance([1,2,3],Iterable))
>
True
print(isinstance('abc',Iterable))
>
True
print(isinstance((i * i for i in range(1,7)),Iterable))
>
True
如果一個可迭代對象可以作爲內置函數next()的實參從而支持惰性推算,那麼該對象被稱爲迭代器(Iterator)對象。
對於range,列表,元組,字符串,字典和合集等可迭代對象,都不可以作爲內置函數next()的實參,而生成器可以。所以,生成器是迭代器的一種。
可以調用內置函數isinstance()判斷一個對象是否是迭代器對象。標準庫模塊collections中的類Iterator用於表示迭代器對象。
L = [1,2,3]
next(L)
>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-613237b05554> in <module>()
      1 L = [1,2,3]
----> 2 next(L)

TypeError: 'list' object is not an iterator

s = 'abc'
next(s)
>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-000efaea8dad> in <module>()
      1 s = 'abc'
----> 2 next(s)

TypeError: 'str' object is not an iterator

from collections import Iterator
print(isinstance(L, Iterator))
print(isinstance(s, Iterator))
>
False
False
print(isinstance((i * i for i in range(1,7)),Iterator))
>
True
可以調用內置函數iter()把不支持惰性推算的可迭代對象轉換爲迭代器對象。
iter_L = iter(L)
iter_s = iter(s)
print(isinstance(iter_L, Iterator))
print(isinstance(iter_s, Iterator))
>
True
True
print(next(iter_L))
print(next(iter_L))
print(next(iter_L))
print(next(iter_L))
>
1
2
3
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-18-6087a98b9770> in <module>()
      2 print(next(iter_L))
      3 print(next(iter_L))
----> 4 print(next(iter_L))

StopIteration: 

print(next(iter_s))
print(next(iter_s))
print(next(iter_s))
print(next(iter_s))
>
a
b
c
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-19-4df9ec750908> in <module>()
      2 print(next(iter_s))
      3 print(next(iter_s))
----> 4 print(next(iter_s))

StopIteration: 

如果一個對象同時實現了特殊方法__iter__()__next__(),那麼該對象也被稱爲迭代器對象。如果將該對象用於for-in語句,for-in語句首先會調用特殊方法__iter__()返回一個可迭代對象,然後不斷調用該可迭代對象的特殊方法__next__()返回下一次迭代的值,直到遇到StopIteration時退出循環。
class MyIterator(object):
    def __init__(self):
        self.data = 0
    def __iter__(self):
        return self
    def __next__(self):
        if self.data > 5:
            raise StopIteration()
        else:
            self.data += 1
            return self.data
for item in MyIterator():
    print(item)
>
1
2
3
4
5
6
class Fib(object):
    def __init__(self):
        self.a,self.b = 0,1
    def __iter__(self):
        return self
    def __next__(self):
        self.a,self.b = self.b,self.a + self.b
        if self.a > 50:
            raise StopIteration()
        return self.a
for item in Fib():
    print(item)
 >
1
1
2
3
5
8
13
21
34 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章