|
1 | 1 | ## 可迭代对象、迭代器、生成器 |
2 | 2 | #### 1.可迭代对象-Iterable |
3 | | -**可以直接作用于for循环的对象称为可迭代对象。**可迭代对象包含一个\_\_iter__方法,或者\_\_getitem__方法,其中\_\_iter__方法返回一个迭代器。可迭代对象一类是集合数据类型,如list, tuple, dict, set, str等;另一类是生成器generator。 |
| 3 | +1. 可以直接作用于for循环的都是可迭代对象,python中的集合数据类型都是可迭代对象(list, tuple, dict, set, str). |
| 4 | +2. 可迭代对象内部实现了\_\_iter__方法,for循环的时候,python解释器会自动调用可迭代对象的iter(),返回一个迭代器。 |
| 5 | +3. 如果没有实现\_\_iter__方法,但是实现了\_\_getitem__方法,python解释器也会创建一个迭代器,尝试从索引0开始获取元素。 |
4 | 6 |
|
5 | | -#### 2.迭代器-Iterator |
6 | | -**可以作用于next()方法的对象都是迭代器,迭代器对象必须实现\_\_next__()方法。** |
| 7 | +##### 总体来说,使用内置iter()函数可以获取迭代器的对象就是可迭代对象。 |
7 | 8 |
|
8 | | -#### 3.生成器-generator |
9 | | -**生成器实现了\_\_iter__方法和\_\_next__()方法,既是可迭代对象也是迭代器。** |
| 9 | +#### 通过\_\_getitem__方法实现可迭代对象: |
| 10 | +```python |
| 11 | +import re |
| 12 | +class Sentence: |
| 13 | + RE_WORD = re.compile(r'\w+') |
10 | 14 |
|
11 | | -#### 4.生成器函数 |
12 | | -**在函数中如果出现yield关键字,那这个函数就是生成器函数,调用生成器函数,返回一个生成器。** |
| 15 | + def __init__(self, text): |
| 16 | + self.text = text |
| 17 | + self.words = self.RE_WORD.findall(text) |
13 | 18 |
|
14 | | -#### 5.原理与实现 |
15 | | -for循环的时候,如果循环的是Iterable可迭代对象,会调用可迭代对象的\_\_iter__(),返回一个迭代器,然后调用迭代器的\_\_next__()方法,直到遇到StopIteration异常跳出循环;如果循环的是一个迭代器,直接调用迭代器的\_\_next__()方法,直到遇到StopIteration异常跳出循环。 |
| 19 | + def __repr__(self): |
| 20 | + return 'Sentence({})'.format(self.text) |
| 21 | + |
| 22 | + def __getitem__(self, index): |
| 23 | + return self.words[index] |
16 | 24 |
|
17 | | -实现一个迭代器和可迭代对象: |
| 25 | +if __name__ == '__main__': |
| 26 | + s = Sentence('"hello how are you!!"') |
| 27 | + for word in s: |
| 28 | + print(word) |
| 29 | +``` |
| 30 | + |
| 31 | +#### 2.迭代器 |
| 32 | +1. 实现了无参数的\_\_next__方法,返回序列中的下一个元素. |
| 33 | +2. 如果没有元素了,那么抛出 StopIteration 异常 |
18 | 34 |
|
| 35 | +##### 总体来说, 可以作用于next()方法的对象都是迭代器,迭代器对象必须实现\_\_next__()方法。 |
| 36 | +##### 创建一个迭代器,通过iter()返回迭代器的方式,创建一个可迭代对象: |
19 | 37 | ```python |
| 38 | +import re |
| 39 | + |
| 40 | +class Sentence: |
| 41 | + RE_WORD = re.compile(r'\w+') |
| 42 | + |
| 43 | + def __init__(self, text): |
| 44 | + self.text = text |
| 45 | + self.words = self.RE_WORD.findall(text) |
| 46 | + |
| 47 | + def __repr__(self): |
| 48 | + return 'Sentence({})'.format(self.text) |
| 49 | + |
| 50 | + def __iter__(self): |
| 51 | + # 可迭代对象实现iter方法,返回一个迭代器 |
| 52 | + return SentenceIterator(self.words) |
| 53 | + |
| 54 | +class SentenceIterator: |
| 55 | + def __init__(self, words): |
| 56 | + self.words = words |
| 57 | + self.index = 0 |
| 58 | + |
| 59 | + def __next__(self): |
| 60 | + # 迭代器必须实现next() |
| 61 | + try: |
| 62 | + word = self.words[self.index] |
| 63 | + except IndexError: |
| 64 | + raise StopIteration() |
| 65 | + self.index += 1 |
| 66 | + return word |
20 | 67 |
|
21 | | -class AIterator(): |
22 | | - def __init__(self,s,e): |
23 | | - self.current = s |
24 | | - self.e = e |
25 | | - def __next__(self): |
26 | | - if self.current < self.e: |
27 | | - self.index = self.current |
28 | | - self.current += 1 |
29 | | - return self.index |
30 | | - else: |
31 | | - raise StopIteration |
32 | | - |
33 | | -class AIterable(): |
34 | | - def __init__(self, s, e): |
35 | | - self.s = s |
36 | | - self.e = e |
37 | | - def __iter__(self): |
38 | | - return AIterator(self.s, self.e) |
39 | | -a = AIterable(0,5) |
40 | | -for i in a: |
41 | | - print(i) |
| 68 | + def __iter__(self): |
| 69 | + # 迭代器可以不用实现iter()方法 |
| 70 | + # 迭代器如果选择实现iter(),直接返回self即可 |
| 71 | + return self |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + s = Sentence('"hello how are you!!"') |
| 76 | + for word in s: |
| 77 | + print(word) |
| 78 | + |
| 79 | +``` |
| 80 | + |
| 81 | +#### 3.生成器函数 |
| 82 | +**在函数中如果出现yield关键字,那这个函数就是生成器函数,调用生成器函数,返回一个生成器。生成器也是可迭代对象。** |
| 83 | + |
| 84 | +```python |
| 85 | +def gen_abc(): |
| 86 | + i = 1 |
| 87 | + while i <= 5: |
| 88 | + yield i |
| 89 | + i += 1 |
| 90 | + |
| 91 | +if __name__ == '__main__': |
| 92 | + g = gen_abc() |
| 93 | + print(g) # <generator object gen_abc at 0x000002328ADFE8E0> |
| 94 | + print(next(g)) # 1 |
| 95 | + print(next(g)) # 2 |
| 96 | + print(next(g)) # 3 |
| 97 | + print(next(g)) # 4 |
42 | 98 | ``` |
| 99 | + |
| 100 | +#### 4.原理 |
| 101 | +for循环的时候,如果循环的是Iterable可迭代对象,会调用可迭代对象的\_\_iter__(),返回一个迭代器,然后调用迭代器的\_\_next__()方法,直到遇到StopIteration异常跳出循环;如果循环的是一个迭代器,直接调用迭代器的\_\_next__()方法,直到遇到StopIteration异常跳出循环。 |
0 commit comments