Python中for循環(huán)可迭代對象迭代器及生成器源碼學習
問題:
之前在學習list和dict相關的知識時,遇到了一個常見的問題:如何在遍歷list或dict的時候正常刪除?例如我們在遍歷dict的時候刪除,會報錯:RuntimeError: dictionary changed size during iteration;而在遍歷list的時候刪除,會有部分元素刪除不完全。
由這個問題又引發(fā)了我對另一個問題的思考:我們通過for循環(huán)去遍歷一個list或dict時,具體是如何for的呢?即for循環(huán)的本質(zhì)是什么?
在查閱了相關資料后,我認識到這是一個和迭代器相關的問題,所以借此機會來詳細認識一下Python中的for循環(huán)、可迭代對象、迭代器和生成器
1. 迭代
“迭代是重復反饋過程的活動,其目的通常是為了逼近所需目標或結(jié)果。”在Python中,可迭代對象、迭代器、for循環(huán)都是和“迭代”密切相關的知識點。
1.1 可迭代對象Iterable
在Python中,稱可以迭代的對象為可迭代對象。要判斷一個類是否可迭代,只需要判斷這個類是否為Iterable類的實例即可:
>>> from collections.abc import Iterable >>> isinstance([], Iterable) True >>> isinstance(123, Iterable) False
上述提供了一個判斷對象是否為可迭代對象的方法,那么一個對象怎么才是可迭代對象呢——只需要該對象的類實現(xiàn)了__iter__()方法即可:
>>> class A: pass >>> isinstance(A(), Iterable) False >>> class B: def __iter__(self): pass >>> isinstance(B(), Iterable) True
由此可見,只要一個類實現(xiàn)了__iter__()方法,那么這個類的實例對象就是可迭代對象。注意這里的__iter__()方法可以沒有任何內(nèi)容。
1.2 迭代器Iterator
在Python中,通過Iterator類與迭代器相對應。相較于可迭代對象,迭代器只是多實現(xiàn)了一個__next__()方法:
>>> from collections.abc import Iterator >>> class C: def __iter__(self): pass def __next__(self): pass >>> isinstance(C(), Iterator) True
顯然,迭代器一定是可迭代對象(因為迭代器同時實現(xiàn)了__iter__()方法和__next__()方法),而可迭代對象不一定是迭代器。
我們來看一下內(nèi)建類型中的可迭代對象是否為迭代器:
>>> isinstance(C(), Iterator) True >>> isinstance([], Iterable) True >>> isinstance([], Iterator) False >>> isinstance('123', Iterable) True >>> isinstance('123', Iterator) False >>> isinstance({}, Iterable) True >>> isinstance({}, Iterator) False
由此可見,str、list、dict對象都是可迭代對象,但它們都不是迭代器。
至此,我們對可迭代對象和迭代器有了一個基本概念上的認識,也知道了有__iter__()和__next__()這兩種方法。但是這兩個魔法方法究竟是如何使用的呢?它們和for循環(huán)又有什么關系呢?
1.3 for循環(huán)
1.3.1 iter()方法和next()方法
iter()方法和next()方法都是Python提供的內(nèi)置方法。對對象使用iter()方法會調(diào)用對象的__iter__()方法,對對象使用next()方法會調(diào)用對象的__next__()方法。下面我們具體看一下它們之間的關系。
1.3.2 iter()和__iter__()
__iter__()方法的作用就是返回一個迭代器,一般我們可以通過內(nèi)置函數(shù)iter()來調(diào)用對象的__iter__()方法
1.2中舉的例子,只是簡單的實現(xiàn)了__iter__()方法,但函數(shù)體直接被pass掉了,本質(zhì)上是沒有實現(xiàn)迭代功能的,現(xiàn)在我們來看一下__iter__()正常使用時的例子:
>>> class A: def __iter__(self): print('執(zhí)行A類的__iter__()方法') return B() >>> class B: def __iter__(self): print('執(zhí)行B類的__iter__()方法') return self def __next__(self): pass >>> a = A() >>> a1 = iter(a) 執(zhí)行A類的__iter__()方法 >>> b = B() >>> b1 = iter(b) 執(zhí)行B類的__iter__()方法
可以看到,對于類A,我們?yōu)樗腳_iter__()方法設置了返回值為B(),而B()就是一個迭代器;
而對于類B,我們在它的__iter__()方法中直接返回了它的實例self,因為它的實例本身就是可迭代對象。
當然這里我們也可以返回其他的迭代器,但是如果__iter__()方法返回的是一個非迭代器,那么當我們調(diào)用iter()方法時就會報錯:
>>> class C: def __iter__(self): pass >>> iter(C()) Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> iter(C()) TypeError: iter() returned non-iterator of type 'NoneType' >>> class D: def __iter__(self): return [] >>> iter(D()) Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> iter(D()) TypeError: iter() returned non-iterator of type 'list'
1.3.3 next()和__next__()
__next__()方法的作用是返回遍歷過程中的下一個元素,如果沒有下一個元素,則會拋出StopIteration異常,一般我們可以通過內(nèi)置函數(shù)next()來調(diào)用對象的__next__()方法
下面我們以list對象為例,來看一下next是如何遍歷的:
>>> l1 = [1, 2, 3] >>> next(l1) Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> next(l1) TypeError: 'list' object is not an iterator
可以看到,當我們直接對列表對象l1使用next()方法時,會報錯’list’ object is not an iterator,顯然list對象并不是迭代器,也就是說它沒有實現(xiàn)__next__()方法,那么我們怎么才能去”對一個列表對象使用next()“呢——根據(jù)我們前面介紹的__iter__()方法,我們知道它會返回一個迭代器,而迭代器是實現(xiàn)了__next__()方法的,所以我們可以先對list對象使用iter__(),獲取到它對應的迭代器,然后對這個迭代器使用next()就可以了:
>>> l1 = [1, 2, 3] >>> l1_iter = iter(l1) >>> type(l1_iter) <class 'list_iterator'> >>> next(l1_iter) 1 >>> next(l1_iter) 2 >>> next(l1_iter) 3 >>> next(l1_iter) Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> next(l1_iter) StopIteration
思考:__next__()為什么要不停地去取出元素,并且在最后去拋出異常,而不是通過對象的長度相關信息來確定調(diào)用次數(shù)?
個人認為是因為我們可以通過next()去手動調(diào)用對象的__next__()方法,而在next()中并沒有判斷對象的長度,所以需要在__next__()去處理
1.3.4 自定義類實現(xiàn)__iter__()和__next__()
下面我們試著通過實現(xiàn)自定義一下list的迭代過程:
首先我們定義一個類A,它是一個可迭代對象,__iter__()方法會返回一個迭代器B(),并且還擁有一個成員變量m_Lst:
>>> class A: def __init__(self, lst): self.m_Lst = lst def __iter__(self): return B(self.m_Lst)
對于迭代器的類B,我們實現(xiàn)它的__iter__()方法和__next__()方法,注意在__next__()方法中我們需要拋出StopIteration異常。此外,它擁有兩個成員變量self.m_Lst和self.m_Index用于迭代遍歷:
>>> class B: def __init__(self, lst): self.m_Lst = lst self.m_Index= 0 def __iter__(self): return self def __next__(self): try: value = self.m_Lst[self.m_Index] self.m_Index += 1 return value except IndexError: raise StopIteration()
至此,我們已經(jīng)完成了迭代器的準備工作,下面我們來實踐一下迭代吧,為了更好地展示這個過程,我們可以加上一些打印:
>>> class A: def __init__(self, lst): self.m_Lst = lst def __iter__(self): print('call A().__iter__()') return B(self.m_Lst) >>> class B: def __init__(self, lst): self.m_Lst = lst self.m_Index= 0 def __iter__(self): print('call B().__iter__()') return self def __next__(self): print('call B().__next__()') try: value = self.m_Lst[self.m_Index] self.m_Index += 1 return value except IndexError: print('call B().__next__() except IndexError') raise StopIteration() >>> l = [1, 2, 3] >>> a = A(l) >>> a_iter = iter(a) call A().__iter__() >>> next(a_iter) call B().__next__() 1 >>> next(a_iter) call B().__next__() 2 >>> next(a_iter) call B().__next__() 3 >>> next(a_iter) call B().__next__() call B().__next__() except IndexError Traceback (most recent call last): File "<pyshell#5>", line 11, in __next__ value = self.m_Lst[self.m_Index] IndexError: list index out of range During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> next(a_iter) File "<pyshell#5>", line 16, in __next__ raise StopIteration() StopIteration
可以看到,我們借助iter()和next()方法能夠很好地將整個遍歷的過程展示出來。至此,我們對可迭代對象、迭代器以及__iter__()和__next__()都有了一定的認識,那么,for循環(huán)和它們有什么關系呢?
1.3.5 探究for循環(huán)
for循環(huán)是我們使用頻率最高的操作之一,我們一般會用它來遍歷一個容器(列表、字典等),這些容器都有一個共同的特點——都是可迭代對象。那么對于我們自定義的類A,它的實例對象a應該也可以通過for循環(huán)來遍歷:
>>> for i in a: print(i) call A().__iter__() call B().__next__() 1 call B().__next__() 2 call B().__next__() 3 call B().__next__() call B().__next__() except IndexError >>> for i in a: pass call A().__iter__() call B().__next__() call B().__next__() call B().__next__() call B().__next__() call B().__next__() except IndexError
通過打印,我們可以清楚的看到:對一個可迭代對象使用for循環(huán)進行遍歷時,for循環(huán)會調(diào)用該對象的__iter__()方法來獲取到迭代器,然后循環(huán)調(diào)用該迭代器的__next__()方法,依次獲取下一個元素,并且最后會捕獲StopIteration異常(這里可以嘗試在類B的__next__()方法最后只捕獲IndexError而不拋出StopIteration,則for循環(huán)此時會無限循環(huán))
既然我們提到了for循環(huán)會自動去捕獲StopIteration異常,當沒有捕獲到StopIteration異常時會無限循環(huán),那么我們是否可以用while循環(huán)來模擬一下這個過程呢?
>>> while True: try: i = next(a_iter) print(i) except StopIteration: print('except StopIteration') break call B().__next__() 1 call B().__next__() 2 call B().__next__() 3 call B().__next__() call B().__next__() except IndexError except StopIteration
到這里,大家應該對for對可迭代對象遍歷的過程有了一定的了解,想要更深入了解的話可以結(jié)合源碼進一步學習(本次學習分享主要是結(jié)合實際代碼對一些概念進行講解,并未涉及到相應源碼)。
2 生成器
迭代器和生成器總是會被同時提起,那么它們之間有什么關聯(lián)呢——生成器是一種特殊的迭代器。
2.1 獲取生成器
當一個函數(shù)體內(nèi)使用yield關鍵字時,我們就稱這個函數(shù)為生成器函數(shù);當我們調(diào)用這個生成器函數(shù)時,Python會自動在返回的對象中添加__iter__()方法和__next__()方法,它返回的對象就是一個生成器。
代碼示例:
>>> from collections.abc import Iterator >>> def generator(): print('first') yield 1 print('second') yield 2 print('third') yield 3 >>> gen = generator() >>> isinstance(gen, Iterator) True
2.2 next(生成器)
既然生成器是一種特殊的迭代器,那么我們對它使用一下next()方法:
>>> next(gen) first 1 >>> next(gen) second 2 >>> next(gen) third 3 >>> next(gen) Traceback (most recent call last): File "<pyshell#19>", line 1, in <module> next(gen) StopIteration
這里我想給這個generator()函數(shù)加一個return,最后會在拋出異常時打印這個返回值(這里我對Python異常相關的知識了解比較少,不太清楚這個問題,以后再補充吧):
>>> from collections.abc import Iterator >>> def generator(): print('first') yield 1 print('second') yield 2 print('third') yield 3 return 'end' >>> gen = generator() >>> isinstance(gen, Iterator) True >>> next(gen) first 1 >>> next(gen) second 2 >>> next(gen) third 3 >>> next(gen) Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> next(gen) StopIteration: end
可以看到,當我們對生成器使用next()方法時,生成器會執(zhí)行到下一個yield為止,并且返回yield后面的值;當我們再次調(diào)用next(生成器)時,會繼續(xù)向下執(zhí)行,直到下一個yield語句;執(zhí)行到最后再沒有yield語句時,就會拋出StopIteration異常
2.3 生成器和迭代器
通過上面的過程,我們知道了生成器本質(zhì)上就是一種迭代器,但是除了yield的特殊外,生成器還有什么特殊點呢——惰性計算。
這里的惰性計算是指:當我們調(diào)用next(生成器)時,每次調(diào)用只會產(chǎn)生一個值,這樣的好處就是,當遍歷的元素量很大時,我們不需要將所有的元素一次獲取,而是每次只取其中的一個元素,可以節(jié)省大量內(nèi)存。(個人理解:這里注意和上面的迭代器的next()區(qū)別開,對于迭代器,雖然每次next()時,也只會返回一個值,但是本質(zhì)上我們已經(jīng)把所有的值存儲在內(nèi)存中了(比如類A和類B的self.m_Lst),但是對于生成器,內(nèi)存中并不會將所有的值先存儲起來,而是每次調(diào)用next()就獲取一個值)
下面我們來看一個實際的例子:輸出10000000以內(nèi)的所有偶數(shù)(注意,如果實際業(yè)務環(huán)境下需要存儲,那就根據(jù)實際情況來,這里只是針對兩者的區(qū)別進行討論)
首先我們通過迭代器來實現(xiàn):(這里直接使用列表)
>>> def iterator(): lst = [] index = 0 while index <= 10000000: if index % 2 == 0: print(index) lst.append(index) index += 1 return lst >>> result = iterator()
然后通過生成器來實現(xiàn):
>>> def generator(): index = 0 while index <= 10000000: if index % 2 == 0: yield index index += 1 >>> gen = generator() >>> next(gen) 0 >>> next(gen) 2 >>> next(gen) 4 >>> next(gen) 6 >>> next(gen) 8
由于采取了惰性運算,生成器也有它的不足:對于列表對象、字典對象等可迭代對象,我們可以通過len()方法直接獲取其長度,但是對于生成器對象,我們只知道當前元素,自然就不能獲取到它的長度信息了。
下面我們總結(jié)一下生成器和迭代器的相同點和不同點:
生成器是一種特殊的迭代器;迭代器會通過return來返回值,而生成器則是通過yield來返回值,對生成器使用next()方法,會在每一個yield語句處停下;迭代器會存儲所有的元素,但是生成器采用的是惰性計算,只知道當前元素。
2.4 生成器解析式
列表解析式是我們常用的一種解析式:(類似的還有字典解析式、集合解析式)
>>> lst = [i for i in range(10) if i % 2 == 1] >>> lst [1, 3, 5, 7, 9]
而生成器解析式和列表解析式類似,我們只需要將[]更換為()即可:(把元組解析式給搶了,hh)
>>> gen = (i for i in range(10) if i % 2 == 1) >>> gen <generator object <genexpr> at 0x00000193E2945A80> >>> next(gen) 1 >>> next(gen) 3 >>> next(gen) 5 >>> next(gen) 7 >>> next(gen) 9 >>> next(gen) Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> next(gen) StopIteration
至此,我們就有了生成器的兩種創(chuàng)造方式:
生成器函數(shù)(yield)返回一個生成器生成器解析式返回一個生成器 3 解決問題
最后回到我們最初的問題:如何在遍歷list或dict的時候正常刪除?
首先我們來探尋一下出錯的原因,以list對象為例:
>>> lst = [1, 2, 3] >>> for i in lst: print(i) lst.remove(i) 1 3
可以看到,我們在遍歷打印列表元素的同時刪除當前元素,實際的輸出和我們需要的輸出并不一樣。以下是個人理解(想更準確地解答這個問題可能需要進一步結(jié)合源碼):
remove刪除列表元素時,列表元素的索引會發(fā)生變化(這是因為Python底層列表是通過數(shù)組實現(xiàn)的,remove方法刪除元素時需要挪動其他元素,具體分析我后續(xù)會補充相關源碼學習筆記,這里先了解即可)
類比我們自定義實現(xiàn)的迭代器,可以看到我們會在__next__()方法中對索引進行遞增:
>>> class A: def __init__(self, lst): self.m_Lst = lst def __iter__(self): print('call A().__iter__()') return B(self.m_Lst) >>> class B: def __init__(self, lst): self.m_Lst = lst self.m_Index= 0 def __iter__(self): print('call B().__iter__()') return self def __next__(self): print('call B().__next__()') try: value = self.m_Lst[self.m_Index] self.m_Index += 1 return value except IndexError: print('call B().__next__() except IndexError') raise StopIteration()
那么我們可以猜測:列表對象對應的迭代器,應該也是會有一個索引成員變量,用于在__next__()方法中進行定位(這里沒看過源碼,只是個人猜想)
當我們使用for循環(huán)遍歷列表對象時,實際上是通過next()方法對其對應的迭代器進行操作,此時由于remove()方法的調(diào)用,導致列表元素的索引發(fā)生了改變(原來元素3的索引是2,刪除元素2之后索引變?yōu)榱?),所以在__next__()方法中,此時需要遍歷的元素索引為1,而元素3頂替了這個位置,所以最后的輸出為1,3。
dict和list類似,不過在遍歷時刪除dict中的元素時會直接報錯,具體原因大家也可以自行分析。
以上就是Python中for循環(huán)可迭代對象迭代器及生成器學習的詳細內(nèi)容,更多關于Python循環(huán)迭代生成器的資料請關注腳本之家其它相關文章!
相關文章
pandas dataframe中雙中括號和單中括號的區(qū)別及說明
這篇文章主要介紹了pandas dataframe中雙中括號和單中括號的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08