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,生成器概念的更多相关文章

  1. python is、==区别;with;gil;python中tuple和list的区别;Python 中的迭代器、生成器、装饰器

    1. is 比较的是两个实例对象是不是完全相同,它们是不是同一个对象,占用的内存地址是否相同 == 比较的是两个对象的内容是否相等 2. with语句时用于对try except finally 的优 ...

  2. python中基于descriptor的一些概念(下)

    @python中基于descriptor的一些概念(下) 3. Descriptor介绍 3.1 Descriptor代码示例 3.2 定义 3.3 Descriptor Protocol(协议) 3 ...

  3. python中基于descriptor的一些概念

    python中基于descriptor的一些概念(上) 1. 前言 2. 新式类与经典类 2.1 内置的object对象 2.2 类的方法 2.2.1 静态方法 2.2.2 类方法 2.3 新式类(n ...

  4. python中基于descriptor的一些概念(上)

    @python中基于descriptor的一些概念(上) python中基于descriptor的一些概念(上) 1. 前言 2. 新式类与经典类 2.1 内置的object对象 2.2 类的方法 2 ...

  5. Python中sorted(iterable, /, *, key=None, reverse=False)的参数中的斜杆是什么意思?

    通过help(sorted)查看sorted的帮助文档,显示如下: Help on built-in function sorted in module builtins: sorted(iterab ...

  6. 终于理解Python中的迭代器和生成器了!

    迭代器和生成器 目录 迭代器和生成器 可迭代对象和迭代器 基础概念 判断 for循环本质 不想用for循环迭代了,如何使用迭代器? 列表推导式 生成器Generator 概念 如何实现和使用? 生成器 ...

  7. Python中的迭代器、生成器

    from collections import Iterable, Iterator 1. 可迭代(iterable)对象 参考官网链接 class I: def __init__(self, v): ...

  8. python中的迭代器与生成器

    迭代器 迭代器的引入 假如我现在有一个列表l=['a','b','c','d','e'],我想取列表中的内容,那么有几种方式? 1.通过索引取值 ,如了l[0],l[1] 2.通过for循环取值 fo ...

  9. python中的迭代、生成器等等

    本人对编程语言实在是一窍不通啊...今天看了廖雪峰老师的关于迭代,迭代器,生成器,递归等等,word天,这都什么跟什么啊... 1.关于迭代 如果给定一个list或tuple,我们可以通过for循环来 ...

随机推荐

  1. 网络数据包发送工具PacketSender中文源码

    在网上发现了一个好用的工具PacketSender,数据包发送器.对于写网络程序来说,有很大的便利性.虽然在linux下,netcat工具也很好用,但是这个也不错. 原本是英文的,给翻译了一下.这是基 ...

  2. centos mysqldb 安装

    yum install mysql-devel.x86_64 yum install python-devel pip install MySQL-python

  3. [黑科技]bit reverse

    写FFT的时候yy出来了这个bit reverse...时间复杂度O(n),常数大概是(a[x>>1]>>1)|((x&1)<<26)的二分之一(-O3下) ...

  4. VRRP虚拟路由器冗余协议

    VRRP(VirtualRouterRedundancyProtocol,虚拟路由冗余协议)是一种容错协议.通常,一个网络内的所有主机都设置一条缺省路由,这样,主机发出的目的地址不在本网段的报文将被通 ...

  5. 【python】python新手必碰到的问题---encode与decode,中文乱码[转]

    转自:http://blog.csdn.net/a921800467b/article/details/8579510 为什么会报错“UnicodeEncodeError:'ascii' codec ...

  6. libreoffice安装

    centos7下libreoffice的安装 #下载安装包wget http://mirrors.ustc.edu.cn/tdf/libreoffice/stable/5.1.1/rpm/x86_64 ...

  7. C# 对象实例几种方法

    //实例方法1 //new CreateCalss cc1 = new CreateCalss(); //实例方法2 //获取当前程序集 Assembly asse = Assembly.GetExe ...

  8. SQL Server 2005中的分区表

    记录笔记: 转自 猪八戒学做网站 SQL Server 2005中的分区表(一):什么是分区表?为什么要用分区表?如何创建分区表? SQL Server 2005中的分区表(二):如何添加.查询.修改 ...

  9. delphi控件属性大全-详解-简介

    http://blog.csdn.net/u011096030/article/details/18716713 button 组件: CAPTION 属性 :用于在按钮上显示文本内容 Cancel ...

  10. PL/Proxy介绍

    PL/Proxy 介绍 一.概述 1.PL/Proxy 是一个采用PL Language语言的数据库分区系统. 目的:轻松访问分区数据库 它的理念是代理远程函数体内指定.函数调用同样标签创建的函数,所 ...