python中的Iterable, Iterator,生成器概念
https://nychent.github.io/articles/2016-05/about-generator.cn
这个深刻
谈起Generator, 与之相关的的概念有 - {list, set, tuple, dict} comprehension and container - iterable - iterator - generator fuction and iterator - generator expression

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Spawn a Process: Chapter 3: Process Based Parallelism
import multiprocessing
import time
from collections import Iterable, Iterator
import dis
from itertools import islice
x = [1, 2, 3]
for i in x:
print i
y = iter(x)
z = iter(x)
print dir(x)
print dir(y)
print next(y)
print next(y)
print next(z)
print next(z)
print type(x)
print isinstance(x, Iterable)
print isinstance(x, Iterator)
print type(y)
print isinstance(y, Iterable)
print isinstance(y, Iterator)
class seq(object):
def __init__(self):
self.gap = 2
self.curr = 1
def __iter__(self):
return self
def next(self):
value = self.curr
self.curr += self.gap
return value
f = seq()
print list(islice(f, 0, 10))
def seq():
gap, curr = 2, 1
while True:
yield curr
curr += gap
f = seq()
print list(islice(f, 0, 10))
def fib():
a, b = 0, 1
while True:
yield b
a, b = b, a + b
print fib
f = fib()
print f
print(next(f), next(f), next(f), next(f), next(f))
def gen():
while True:
value = yield
print(value)
g = gen()
next(g)
g.send("hahahha")
next(g)
python中的Iterable, Iterator,生成器概念的更多相关文章
- python is、==区别;with;gil;python中tuple和list的区别;Python 中的迭代器、生成器、装饰器
1. is 比较的是两个实例对象是不是完全相同,它们是不是同一个对象,占用的内存地址是否相同 == 比较的是两个对象的内容是否相等 2. with语句时用于对try except finally 的优 ...
- python中基于descriptor的一些概念(下)
@python中基于descriptor的一些概念(下) 3. Descriptor介绍 3.1 Descriptor代码示例 3.2 定义 3.3 Descriptor Protocol(协议) 3 ...
- python中基于descriptor的一些概念
python中基于descriptor的一些概念(上) 1. 前言 2. 新式类与经典类 2.1 内置的object对象 2.2 类的方法 2.2.1 静态方法 2.2.2 类方法 2.3 新式类(n ...
- python中基于descriptor的一些概念(上)
@python中基于descriptor的一些概念(上) python中基于descriptor的一些概念(上) 1. 前言 2. 新式类与经典类 2.1 内置的object对象 2.2 类的方法 2 ...
- Python中sorted(iterable, /, *, key=None, reverse=False)的参数中的斜杆是什么意思?
通过help(sorted)查看sorted的帮助文档,显示如下: Help on built-in function sorted in module builtins: sorted(iterab ...
- 终于理解Python中的迭代器和生成器了!
迭代器和生成器 目录 迭代器和生成器 可迭代对象和迭代器 基础概念 判断 for循环本质 不想用for循环迭代了,如何使用迭代器? 列表推导式 生成器Generator 概念 如何实现和使用? 生成器 ...
- Python中的迭代器、生成器
from collections import Iterable, Iterator 1. 可迭代(iterable)对象 参考官网链接 class I: def __init__(self, v): ...
- python中的迭代器与生成器
迭代器 迭代器的引入 假如我现在有一个列表l=['a','b','c','d','e'],我想取列表中的内容,那么有几种方式? 1.通过索引取值 ,如了l[0],l[1] 2.通过for循环取值 fo ...
- python中的迭代、生成器等等
本人对编程语言实在是一窍不通啊...今天看了廖雪峰老师的关于迭代,迭代器,生成器,递归等等,word天,这都什么跟什么啊... 1.关于迭代 如果给定一个list或tuple,我们可以通过for循环来 ...
随机推荐
- (转载)XML解析之-XStream解析
转载来源:http://hwy584624785.iteye.com/blog/1168680 本例使用XStream生成一个xml文件,再发序列化xml文件内容. XStream是一个简单的类库,可 ...
- BZOJ 4423: [AMPPZ2013]Bytehattan
Sol 对偶图+并查集. 思路非常好,将网格图转化成对偶图,在原图中删掉一条边,相当于在对偶图中连上一条边(其实就是网格的格点相互连边),每次加边用并查集维护就可以了. 哦对,还要注意边界就是网格外面 ...
- ListView滑动位置精准记忆
需求场景:一个ListView页面,滑动阅读到某一位置,然后退出页面,下次再进入页面的时候,想要直接滑动到上次阅读的位置. 方案1:页面退出的时候,ListView.getFirstVisiblePo ...
- [转]mysql分布式方案-分库拆表
来源:http://kissthink.com/archive/mysql-distributed-programs---and-warehouses-split-table.html 分库& ...
- 3D音效
摘自:http://baike.baidu.com/view/1330437.htm?fr=aladdin 3D音效就是用扬声器仿造出似乎存在但是虚构的声音.例如扬声器仿造头顶上有一架飞机从左至右飞过 ...
- 堆栈指针 ---delete 使用
对拥有堆中一个有效对象的地址的指针进行删除操作的结果,是将这个堆内存的状态从“使用中” 变为“可用”(此时的可用就是指可以调用内存)释放了,可以再次覆盖此处;; 对指针内存进行删除操作后,指针 ...
- Selenium2(webdirver)入门之环境搭建(Java版)
一.开发环境: 1.JDK1.6 2.Eclipse:Version: Kepler Service Release 1,下载地址:http://www.eclipse.org/downloads/ ...
- yum install 安装时报yum doesn't have enough cached data to continue.
yum install 安装时报yum doesn't have enough cached data to continue. 安装epel,yum -y install epel-release后 ...
- selenium 配合sikuli script操作高德地图
会不会使用工具,是一般QA和高级QA的区别 ---To be crazy Java就是好,开源框架遍地都是,各种niubility的jar包,各种神器,真是不亦乐乎. 今天研究一下基于图片识别作为对象 ...
- perl的一些函数(二)
1. tr 转换 转换不是替换(tr///==y///) tr/searchlist/replacementlist/用于查找某个一个字符串,并用replacementlist替换,可以使用正则表达式 ...