Python Iterables Iterators Generators
container
某些对象包含其它对象的引用,这个包含其它对象引用的对象叫容器.例如list可以包含int对象,或者由其它数据类型(或数据结构)的对象组成一个list.
对其他对象的引用是容器值的一部分,如果这个引用是对可变对象的引用,那么这个可变对象值改变的时候,容器中的值也会变
Iterables
一次可以返回一个成员的对象,就是可迭代对象.
任何实现了__iter__()方法或有__getitem__()方法实现序列语义的类的对象.
序列(sequence):一个支持使用整形索引(indices)利用 getitem()实现元素访问并且定义了可以返回序列长度的__len__()方法的可迭代对象.典型内置:list, str, tuple, and bytes.
可迭代对象可以使用在for循环或者在许多其它需要一个序列的地方(如zip().map()...).
当可迭代对象做为参数传给内置iter()函数时,它会返回一个该对象的迭代器.
Iterators
迭代器是表示数据流的对象.重复调用迭代器的__next__()方法或将迭代器传给BIF(built-in function)next(),会返回流中的连续项,当没有可访问的数据会抛出StopIteration的异常.
迭代器也需要有__iter__()方法,该方法返回迭代器对象本身,因此每个迭代器是可迭代的,并且可以在大多数接受其它迭代的地方使用.
BIF iter():该函数返回一个定义了一次访问容器中一个元素的__next__()方法的迭代器.
BIF next():通过调用迭代器(传给next的参数)的__next__()方法取回下一个元素.
Generators
生成器是用于创建迭代器的简单而强大的工具,是一个返回生成器迭代器的函数.它和普通函数不同在于返回值时使用yield表达式,在for-loop中可以用来产生一系列值.
术语生成器通常指生成器函数,但是某些上下文中可能也指生成器迭代器.上面也提到了生成器迭代器,什么是生成器迭代器?
被一个生成器函数创建的对象就叫生成器迭代器.
本质上生成器是函数,生成器迭代器是对象.
说这么多不如栗子来得贴切.
栗子
>>> s = 'abc'
>>> it = iter(s)
>>> it
<iterator object at 0x00A1DB50>
>>> next(it)
'a'
>>> next(it)
'b'
>>> next(it)
'c'
>>> next(it)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
next(it)
StopIteration
s是可迭代对象str,it是迭代器,用BIF next()可以让it移动,最终抛出异常.
了解了迭代器的机制,可以自己添加迭代器行为到类里面:
class Reverse:
"""Iterator for looping over a sequence backwards."""
def __init__(self, data):
self.data = data
self.index = len(data)
def __iter__(self):
return self
def __next__(self):
if self.index == 0:
raise StopIteration
self.index = self.index - 1
return self.data[self.index]
output:
>>> rev = Reverse('spam')
>>> iter(rev)
<__main__.Reverse object at 0x00A1DB50>
>>> for char in rev:
... print(char)
...
m
a
p
s
这个类用于将串翻转输出,一次输出一个字符.它定义了一个__iter__()方法,返回一个有__next__()方法的对象,这里就是它本身.
这个类有__iter__(),所以rev是可迭代对象,iter()直接返回self.上面不是说一次可以返回一个成员(元素)的对象才是可迭代对象吗?它怎么实现的?
rev有__next__()方法,所以他是迭代器对象,它就可以实现一次返回一个值.为什么要这样实现,而不用__iter__()直接一次返回一个值?
因为rev不仅是可迭代对象,它还是迭代器对象.
If the class defines next(), then iter() can just return self
**The iterator objects themselves are required to support the following two methods, which together form the iterator protocol:
iterator.iter()
Return the iterator object itself. This is required to allow both containers and iterators to be used with the for and in statements. This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.
iterator.next()
Return the next item from the container. If there are no further items, raise the StopIteration exception. This method corresponds to the tp_iternext slot of the type structure for Python objects in the Python/C API.**
以上可以看出,这是迭代器协议规定的.这也说明了迭代器对象一定是可迭代的,但是可迭代对象不一定是迭代器对象.
迭代器是表示数据流的对象,它可以直接给for-loop或者next()使用,但是可迭代对象要给iter(),然后才能返回一个迭代器对象.(待添加例子)
def reverse(data):
for index in range(len(data)-1, -1, -1):
yield data[index]
output:
>>> for char in reverse('golf'):
... print(char)
...
f
l
o
g
这里reverse()是一个生成器函数,实现上栗相同的功能.reverse('golf')就是一个生成器迭代器对象.所以可以直接给for-loop使用
这些概念容易混淆,可能是由于中文字符的主观原因,什么什么器的.
再强调一下,可迭代对象(iterables),迭代器(iterators)都是对象,而生成器(generators)通常说的是生成器函数,但是也有上下文是生成器迭代器对象.
REF:
https://docs.python.org/3/reference/datamodel.html?highlight=container
https://docs.python.org/3/tutorial/classes.html#iterators
https://docs.python.org/3/library/stdtypes.html#iterator-types
https://docs.python.org/3/glossary.html#term-iterable
https://docs.python.org/3/glossary.html#term-generator
https://nvie.com/posts/iterators-vs-generators/
Python Iterables Iterators Generators的更多相关文章
- Iterators & Generators in depth
Iterators & Generators in depth https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/It ...
- Meet python: little notes 4 - high-level characteristics
Source: http://www.liaoxuefeng.com/ ♥ Slice Obtaining elements within required range from list or tu ...
- Python 学习教程汇总
Python快速教程http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html简明Python教程https://bop.molun.ne ...
- python的迭代器、生成器、装饰器
迭代器.生成器.装饰器 在这个实验里我们学习迭代器.生成器.装饰器有关知识. 知识点 迭代器 生成器 生成器表达式 闭包 装饰器 实验步骤 1. 迭代器 Python 迭代器(Iterators)对象 ...
- Python中多使用迭代器
英文原文出处:Use More Iterators 本文介绍将代码转换为使用迭代器的原因和实用技巧. 我最喜欢的Python语言的特色之一是生成器,它们是非常有用的,然而当阅读开源代码时,我很少遇到它 ...
- Python 开发面试题
Python部分 将一个字符串逆序,不能使用反转函数 求从10到100中能被3或5整除的数的和 What is Python? What are the benefits of using Pytho ...
- 【Python注意事项】如何理解python中间generator functions和yield表情
本篇记录自己的笔记Python的generator functions和yield理解表达式. 1. Generator Functions Python支持的generator functions语 ...
- Principle of Computing (Python)学习笔记(5) BFS Searching + Zombie Apocalypse
1 Generators Generator和list comprehension非常类似 Generators are a kind of iterator that are defined l ...
- Python.URLs
1. The Future of Asynchronous IO in Python https://medium.com/@paulcolomiets/the-future-of-asynchron ...
随机推荐
- php发送邮箱
/** * 系统邮件发送函数 * @param string $tomail 接收邮件者邮箱 * @param string $name 接收邮件者名称 * @param string $subjec ...
- App拉起小程序提示跳转失败
App拉起小程序提示跳转失败 req.userName = "gh_8afldfalsejw"; // 小程序的原始ID,注意不是Appid
- NTFS文件系统详细分析
NTFS文件系统详细分析 第一部分 什么是NTFS文件系统 想要了解NTFS,我们首先应该认识一下FAT.FAT(File Allocation Table)是“文件分配表”的意思.对我们来说 ...
- PHP函数ip2long转换IP时数值太大而产生负数
// 使用 sprintf 返回无符号十进制数 $long = sprintf("%u",ip2long($ip)); // 先转换为二进制然后在转换为十进制 $long = bi ...
- 加壳软件-Virbox Protector Standalone
Virbox Protector Standalone 加壳工具 防止代码反编译,更安全,更方便 产品简介 Virbox Protector Standalone提供了强大的代码虚拟化.高级混淆与智能 ...
- 如何创建自己的composer包
composer中文网 :https://www.phpcomposer.com/ 一.前期准备: composer 安装 Windows安装: 1.下载安装包,https://getcomposer ...
- centos修改时区并同步时间
查看服务器时间及所在时区 [root@localhost ~]# date -R Fri, 07 Dec 2018 04:38:28 -0500 修改时区 先使用 tzselect 根据提示选择所在地 ...
- patA1059 Prime Factors
这个问题叫做质因子分解,花了大概两个小时写对了.这道题细节挺多的,书上提到了几点,一个是n=1的话需要特判.有一个很容易错的点就是n一开始要先用一个变量保存起来,不保存的话后面有点麻烦,所以建议还是先 ...
- 接口自动化框架(java)--1.项目概述
项目github地址: https://github.com/tianchiTester/API_AutoFramework 这套框架的报告是自己封装的 1.测试基类TestBase: 接口请求的te ...
- 201808_summary
@Consumes @Produces分别表示入参和出参数吗 可以这样讲.但是不是很到位.是限定作用,类似于filterconsumes: 指定处理请求的提交内容类型(Content-Type),例如 ...