【代码】Fluent Python

本文深入探讨了Python中的迭代器和生成器。详细介绍了Sentence类、迭代器的检查方法、可迭代对象与迭代器的区别,以及生成器函数的工作原理。通过示例解释了iter函数、上下文管理器、@contextmanager装饰器和协程的概念,强调了它们在并发和资源管理中的应用。同时,提到了concurrent.futures模块在并行下载中的使用,以及GIL对多线程的影响。
摘要由CSDN通过智能技术生成

14.1 Sentence类第1版:单词序列
从 Python 3.4 开始,检查对象 x 能否迭代,最准确的方法是:调用 iter(x) 函数,如果不可迭代,再处理 TypeError 异常。这比使用 isinstance(x, abc.Iterable) 更准确,因为 iter(x) 函数会考虑到遗留的__getitem__方法,而 abc.Iterable 类则不考虑。

>>> class F():
	def __iter__(self):
		pass
>>> f = F()
>>> iter(f)
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    iter(f)
TypeError: iter() returned non-iterator of type 'NoneType'
>>> isinstance(f, collections.abc.Iterable)
True

>>> class G():
	def __getitem__(self):
		pass
>>> g = G()
>>> iter(g)
<iterator object at 0x000001BA5F099A20>
>>> isinstance(g, collections.abc.Iterable)
False

14.3 典型的迭代器
构建可迭代的对象和迭代器时经常会出现错误,原因是混淆了二者。要知道,可迭代的对象有个__iter__方法,每次都实例化一个新的迭代器;而迭代器要实现__next__方法,返回单个元素,此外还要实现__iter__方法,返回迭代器本身。因此,迭代器可以迭代,但是可迭代的对象不是迭代器。

14.4 生成器函数
可迭代对象中:
__iter__方法调用迭代器类的构造方法创建一个迭代器并将其返回:

def__iter__(self): 
	return SentenceIterator(self.words)

迭代器可以是生成器对象,每次调用__iter__方法都会自动创建,因为这里的__iter__方法是生成器函数:

def__iter__(self):
	for word in self.words: 
		yield word

14.12 深入分析iter函数
内置函数 iter 的文档中有个实用的例子。这段代码逐行读取文件,直到遇到空行或者到达文件末尾为止:

with open('mydata.txt') as fp:
	for line in iter(fp.readline, '\n'):
		process_line(line)

用print(line)实测发现,必须要有至少一个整个空行,否则到达结尾也不会结束

14 杂谈
。。。。。。。
生成器与迭代器的语义对比
。。。。。。。
第三方面是概念。
根据《设计模式:可复用面向对象软件的基础》一书的定义,在典型的迭代器设计模式中,迭代器用于遍历集合,从中产出元素。迭代器可能相当复杂,例如,遍历树状数据结构。但是,不管典型的迭代器中有多少逻辑,都是从现有的数据源中读取值;而且,调用 next(it) 时,迭代器不能修改从数据源中读取的值,只能原封不动地产出值。
而生成器可能无需遍历集合就能生成值,例如 range 函数。即便依附了集合,生成器不仅能产出集合中的元素,还可能会产出派生自元素的其他值。enumerate 函数是很好的例子。根据迭代器设计模式的原始定

Python’s simplicity lets you become productive quickly, but this often means you aren’t using everything it has to offer. With this hands-on guide, you’ll learn how to write effective, idiomatic Python code by leveraging its best—and possibly most neglected—features. Author Luciano Ramalho takes you through Python’s core language features and libraries, and shows you how to make your code shorter, faster, and more readable at the same time. Many experienced programmers try to bend Python to fit patterns they learned from other languages, and never discover Python features outside of their experience. With this book, those Python programmers will thoroughly learn how to become proficient in Python 3. This book covers: Python data model: understand how special methods are the key to the consistent behavior of objects Data structures: take full advantage of built-in types, and understand the text vs bytes duality in the Unicode age Functions as objects: view Python functions as first-class objects, and understand how this affects popular design patterns Object-oriented idioms: build classes by learning about references, mutability, interfaces, operator overloading, and multiple inheritance Control flow: leverage context managers, generators, coroutines, and concurrency with the concurrent.futures and asyncio packages Metaprogramming: understand how properties, attribute descriptors, class decorators, and metaclasses work Table of Contents Part I. Prologue Chapter 1. The Python Data Model Part II. Data structures Chapter 2. An array of sequences Chapter 3. Dictionaries and sets Chapter 4. Text versus bytes Part III. Functions as objects Chapter 5. First-class functions Chapter 6. Design patterns with first-class functions Chapter 7. Function decorators and closures Part IV. Object Oriented Idioms Chapter 8. Object references, mutability and recycling Chapter 9. A Pythonic object Chapter 10. Sequence hacking, hashing and slicing Chapter 11. Interfaces: from protocols to ABCs Chapter 12. Inheritance: for good or for worse Chapter 13. Operator overloading: doing it right Part V. Control flow Chapter 14. Iterables, iterators and generators Chapter 15. Context managers and else blocks Chapter 16. Coroutines Chapter 17. Concurrency with futures Chapter 18. Concurrency with asyncio Part VI. Metaprogramming Chapter 19. Dynamic attributes and properties Chapter 20. Attribute descriptors Chapter 21. Class metaprogramming Appendix A. Support scripts
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值