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循环来 ...
随机推荐
- net-snmp5.7.3移植到arm-linux平台
net-snmp5.7.3移植到arm-linux平台 本次交叉编译在ubuntu 15.04 64位系统下进行. 准备工作 在编译移植前有几项准备工作需要完成. 1下载net-snmp 5.7.3源 ...
- Python自动化之线程进阶篇
拓展知识 什么是CPU-bound(计算密集型) 和I/O bound(I/O密集型) ? I/O bound 指的是系统的CPU效能相对硬盘/内存的效能要好很多,此时,系统运作,大部分的状况是 CP ...
- 获取客户端IP
function getIP(){ $ip = ""; if (getenv("HTTP_CLIENT_IP") && strcasecmp(g ...
- GIT文件的三种状态
对于任何一个文件,在 Git 内都只有三种状态:已提交(committed),已修改(modified)和已暂存(staged).已提交表示该文件已经被安全地保存在本地数据库 中了:已修改表示修改了某 ...
- 3分钟,9个Q&A让你快速知道Docker到底是什么
不论是Google.Amazon.Microsoft.VMware都纷纷拥戴,加入Docker和Container所掀起的新时代云端虚拟化行列,这两项技术成为了IT界的新趋势.Docker和Conta ...
- Oracle占用8080端口问题的解决
可能在本地同时安装过Tomcat和Oracle的人都会知道,安装完Oracle后,会发现Tomcat的8080端口已经被Oracle占用了. 完全安装Oracle数据库后,当我们访问8080端口时,会 ...
- 【K8s】Kubernetes 最近正在看的资料
中国移动Kubernetes多集群统一管理实践: http://www.tuicool.com/articles/FrqQrqI#c-22517 一种新的进入容器的方式: WebSocket + D ...
- ACM/ICPC 之 DFS求解欧拉通路路径(POJ2337)
判断是欧拉通路后,DFS简单剪枝求解字典序最小的欧拉通路路径 //Time:16Ms Memory:228K #include<iostream> #include<cstring& ...
- 13. javacript高级程序设计-事件
1. 事件 1.1 事件流 事件流描述的是从页面中接受事件的顺序,IE的事件是冒泡流,而Netscape Communicator的事件流是事件捕捉流. 1.1.1 事件冒泡 <!DOCTYPE ...
- effective OC2.0 52阅读笔记(四 协议与分类)
23 通过委托与数据源协议进行对象间通信 总结:委托模式的常规委托模式中,信息从类Class流向受委托者delegate.数据源模式,信息从数据源datasource流向class.数据源和受委托者可 ...