在 python 中我们常用 for in 来遍历 list, set, dict, str 等。

for in 的本质就干了两件事:

  1. 调用 _iter_() 获取迭代器;
  2. 调用 next() 直到 StopIteration 异常; (python3 中是 _next_())

迭代器

我们先了解几个概念:

  • Iterable: 可迭代对象
  • Iterator: 迭代器

我们先看看 Iterable 的实现

from collections import Iterable

help(Iterable)

class Iterable(__builtin__.object)
| Methods defined here:
|
| __iter__(self)
|
| ----------------------------------------------------------------------
| Class methods defined here:
|
| __subclasshook__(cls, C) from abc.ABCMeta
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __abstractmethods__ = frozenset(['__iter__'])
|
| __metaclass__ = <class 'abc.ABCMeta'>
| Metaclass for defining Abstract Base Classes (ABCs).
|
| Use this metaclass to create an ABC. An ABC can be subclassed
| directly, and then acts as a mix-in class. You can also register
| unrelated concrete classes (even built-in classes) and unrelated
| ABCs as 'virtual subclasses' -- these and their descendants will

再看看 Iterator 的实现

from collections import Iterator

help(Iterator)

class Iterator(Iterable)
| Method resolution order:
| Iterator
| Iterable
| __builtin__.object
|
| Methods defined here:
|
| __iter__(self)
|
| next(self)
| Return the next item from the iterator. When exhausted, raise StopIteration
|
| ----------------------------------------------------------------------
| Class methods defined here:
|
| __subclasshook__(cls, C) from abc.ABCMeta
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __abstractmethods__ = frozenset(['next'])
|
| ----------------------------------------------------------------------
| Data descriptors inherited from Iterable:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Data and other attributes inherited from Iterable:
|
| __metaclass__ = <class 'abc.ABCMeta'>
| Metaclass for defining Abstract Base Classes (ABCs).
|
| Use this metaclass to create an ABC. An ABC can be subclassed
| directly, and then acts as a mix-in class. You can also register
| unrelated concrete classes (even built-in classes) and unrelated
| ABCs as 'virtual subclasses' -- these and their descendants will
| be considered subclasses of the registering ABC by the built-in
| issubclass() function, but the registering ABC won't show up in
| their MRO (Method Resolution Order) nor will method
| implementations defined by the registering ABC be callable (not
| even via super()).

从继承关系来看,所有的 Iterator(迭代器)都是 Iterable(可迭代对象),

从实现角度看 Iterator 新增了 next() 方法。

判断是 Iterator 还是 Iterable

  • 凡是可以 for 循环的,都是 Iterable;
  • 凡是可以 next() 的,都是 Iterator;
  • list, tuple, dict, str, set 都不是 Iterator,但是可以通过 _iter_() 返回一个 Iterator 对象
from collections import Iterator, Iterable

isinstance([1,], Iterator)    // False
isinstance((1,), Iterator) // False
isinstance({}, Iterator) // False
isinstance("abc", Iterator) // False
isinstance(set([]), Iterator) // False isinstance([1,], Iterable) // True
isinstance((1,), Iterable) // True
isinstance({}, Iterable) // True
isinstance("abc", Iterable) // True
isinstance(set([]), Iterable) // True dir([]) // 没有 next() 方法
dir([].__iter__()) // 有 next() 方法

生成器

将完了迭代器,我们再说说生成器,这里引用廖雪峰博客里的介绍:

通过列表生成式,我们可以直接创建一个列表。但是,受到内存限制,列表容量肯定是有限的。

而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,

如果我们仅仅需要访问前面几个元素,那后面绝大多数元素占用的空间都白白浪费了。

所以,如果列表元素可以按照某种算法推算出来,那我们是否可以在循环的过程中不断推算出后续的元素呢?

这样就不必创建完整的list,从而节省大量的空间。在Python中,这种一边循环一边计算的机制,

称为生成器(Generator)。

生成器的创建很简单,可以通过推导列表创建:

 g = (x * x for x in range(10))  // 使用 [] 返回的是 list, () 返回的是 generator

还有一种方式是通过 yield 关键字生成。

先看看生成器的实现:

<genexpr> = class generator(object)
| Methods defined here:
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
|
| __iter__(...)
| x.__iter__() <==> iter(x)
|
| __repr__(...)
| x.__repr__() <==> repr(x)
|
| close(...)
| close() -> raise GeneratorExit inside generator.
|
| next(...)
| x.next() -> the next value, or raise StopIteration
|
| send(...)
| send(arg) -> send 'arg' into generator,
| return next yielded value or raise StopIteration.
|
| throw(...)
| throw(typ[,val[,tb]]) -> raise exception in generator,
| return next yielded value or raise StopIteration.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| gi_code
|
| gi_frame
|
| gi_running

可以发现生成器较迭代器多了 send, throw 等方法。

send

这里重点介绍下 send 方法,我们知道在使用迭代器时,遇到 yield 关键字

会退出来,下一迭代时会继续执行。先看个例子:

def MyGenerator():
value = yield 1
value = yield value gen = MyGenerator()
print gen.next() // print 1
print gen.next() // print None

我们看看具体执行过程:

  • 调用 next() 方法,走到 yield 1 退出,注意这个时候还没有走到 value 的 赋值操作(即: value = yield 1 只执行了右侧部分)
  • 调用 next() 方法,继续上次的代码执行(即:value = yield 1 只执行了右侧的赋值部分)
  • 由于 yield 并没有返回值,所以 value = None
  • 返回 None, 并打印

修改下上面的例子:

def MyGenerator():
value = yield 1
value = yield value gen = MyGenerator()
print gen.next() // print 1
print gen.send(2) // print 2

send 方法是指定的是上一次被挂起的yield语句的返回值,这么说有点抽象,我们看执行过程:

  • 调用 next() 方法,走到 yield 1 退出,注意这个时候还没有走到 value 的 赋值操作(即: value = yield 1 只执行了右侧部分)
  • 调用 send(2) 方法,继续上次的代码执行(即:value = yield 1 只执行了右侧的赋值部分)
  • value 使用 send 传的值,即: value = 2
  • 返回 2, 并打印

协程

协程就是利用 yield 和生成器的 send() 方法实现的。

python 迭代器,生成器的更多相关文章

  1. Python迭代器生成器与生成式

    Python迭代器生成器与生成式 什么是迭代 迭代是重复反馈过程的活动,其目的通常是为了逼近所需目标或结果.每一次对过程的重复称为一次"迭代",而每一次迭代得到的结果会作为下一次迭 ...

  2. Python 迭代器&生成器

    1.内置参数     Built-in Functions     abs() dict() help() min() setattr() all() dir() hex() next() slice ...

  3. python 迭代器 生成器

    迭代器 生成器 一 什么是迭代器协议 1.迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代 (只能往后走不能往前 ...

  4. Python 迭代器&生成器,装饰器,递归,算法基础:二分查找、二维数组转换,正则表达式,作业:计算器开发

    本节大纲 迭代器&生成器 装饰器  基本装饰器 多参数装饰器 递归 算法基础:二分查找.二维数组转换 正则表达式 常用模块学习 作业:计算器开发 实现加减乘除及拓号优先级解析 用户输入 1 - ...

  5. python迭代器,生成器,推导式

    可迭代对象 字面意思分析:可以重复的迭代的实实在在的东西. list,dict(keys(),values(),items()),tuple,str,set,range, 文件句柄(待定) 专业角度: ...

  6. 4.python迭代器生成器装饰器

    容器(container) 容器是一种把多个元素组织在一起的数据结构,容器中的元素可以逐个地迭代获取,可以用in, not in关键字判断元素是否包含在容器中.通常这类数据结构把所有的元素存储在内存中 ...

  7. python迭代器生成器

    1.生成器和迭代器.含有yield的特殊函数为生成器.可以被for循环的称之为可以迭代的.而可以通过_next()_调用,并且可以不断返回值的称之为迭代器 2.yield简单的生成器 #迭代器简单的使 ...

  8. Python迭代器生成器,私有变量及列表字典集合推导式(二)

    1 python自省机制 这个是python一大特性,自省就是面向对象的语言所写的程序在运行时,能知道对象的类型,换句话说就是在运行时能获取对象的类型,比如通过 type(),dir(),getatt ...

  9. Python迭代器生成器,模块和包

      1.迭代器和生成器 2.模块和包 1.迭代器 迭代器对象要求支持迭代器协议的对象,在Python中,支持迭代器协议就是实现对象的__iter__()和__next__()方法.    其中__it ...

  10. python迭代器生成器-迭代器和list区别

    迭代 生成 for循环遍历的原理 for循环遍历的原理就是迭代,in后面必须是可迭代对象 为什么要有迭代器 对于序列类型:字符串.列表.元组,我们可以使用索引的方式迭代取出其包含的元素.但对于字典.集 ...

随机推荐

  1. 第30月第18天 autolayout代码

    1.上下左右 [tipsLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; { id view1 = tipsLabel; id view2 ...

  2. 浅入深出Vue系列

    浅入深出Vue导航 导航帖,直接点击标题即可. 文中所有涉及到的资源链接均在最下方列举出来了. 前言 基础篇 浅入深出Vue:工具准备之WebStorm搭建及配置 浅入深出Vue之工具准备(二):Po ...

  3. python多任务抓取图片

    import re import urllib.request import gevent def download(image_download, images_path,i): headers = ...

  4. A Discriminative Feature Learning Approach for Deep Face Recognition

    url: https://kpzhang93.github.io/papers/eccv2016.pdf year: ECCV2016 abstract 对于人脸识别任务来说, 网络学习到的特征具有判 ...

  5. numpy数组取每一列的数据

    也可以运用到列表中,a原本是一个列表的嵌套,将a转为了数组进行此操作,可以取固定的值,这就是numpy的好处.

  6. mysql windows 安装 错误

    1 免压缩版安装会经常提示需要c++库,还是要用安装版. 2 安装板默认在c盘,修改目录要注意目录权限. 3 要把datadir的data目录全部考到新目录下.

  7. css奇技淫巧-色彩渐变与动态渐变

    来源 css渐变 CSS 中设置的渐变是 gradient 数据类型,它是一种特别的image数据类型.使用background-image设置,可叠加设置多个: CSS3 定义了两种类型的渐变(gr ...

  8. LeetCode 9. Palindrome Number(c语言版)

    题目: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same ...

  9. IDEA启动maven项目

    一.安装IDEA 自行到官网下载,有条件请购买版权 地址:https://www.jetbrains.com/idea/ 二.修改快捷键(如果不是eclipse老用户请忽略这段) 左上角File→Sr ...

  10. 学习笔记_J2EE_Spring(一)_入门

    3.      Spring概述 3.1.   Spring是什么 Spring是一个优秀的高可用的JavaEE轻量级开发框架.提供一站式开发解决方案. 3.2.   Spring框架出现的背景 在世 ...