已读35%
预计阅读本页时间:-
预计阅读本页时间:-
Other Iterator Topics
We’ll learn more about both list comprehensions and iterators in Chapter 20, in conjunction with functions, and again in Chapter 29 when we study classes. As you’ll see later:
广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元
- User-defined functions can be turned into iterable generator functions, with yield statements.
- List comprehensions morph into iterable generator expressions when coded in parentheses.
- User-defined classes are made iterable with __iter__ or __getitem__ operator overloading.
In particular, user-defined iterators defined with classes allow arbitrary objects and operations to be used in any of the iteration contexts we’ve met here.