python iterator generator yield
来源:互联网 发布:2017淘宝红包口令 编辑:程序博客网 时间:2024/04/19 15:33
个人理解,欢迎指正
1、iterator
container.__iter__() Return an iterator object. The object is required to support the iterator protocol described below. If a container supports different types of iteration, additional methods can be provided to specifically request iterators for those iteration types. (An example of an object supporting multiple forms of iteration would be a tree structure which supports both breadth-first and depth-first traversal.) This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API. 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.
一共三个方法:
container.__iter__():可以对容器对象调用。比如Iter=[1,2,3].__iter__(),于是生成了一个iterator对象,这个语句等价于Iter=iter([1,2,3])
iterator.__iter__():同上,对迭代器对象生成一个迭代器对象,但是会继承原来迭代器的状态
iterator.next():迭代器的next()方法,顺序访问iter的元素。#注意迭代器只有这个三个attribute,没有切片属性,不能进行切片,print Iter[1] --》TypeError: 'listiterator' object has no attribute '__getitem__'
#encoding:utf8Iter = iter([1, 2, 3, 4]) # <=>Iter=[1,2,3,4].__iter__()nIter = iter(Iter) # <=>nIter=Iter.__iter__()print Iter.next()print nIter.next()#继承了Iter的状态for i in Iter: print i#print Iter.next() --> StopIteration'''1234'''
2、generator
generator.next()¶ Starts the execution of a generator function or resumes it at the last executed yield expression. When a generator function is resumed with a next() method, the current yield expression always evaluates to None. The execution then continues to the next yield expression, where the generator is suspended again, and the value of the expression_list is returned to next()‘s caller. If the generator exits without yielding another value, a StopIteration exception is raised.
generator.send(value) Resumes the execution and “sends” a value into the generator function. The value argument becomes the result of the current yield expression. The send() method returns the next value yielded by the generator, or raises StopIteration if the generator exits without yielding another value. When send() is called to start the generator, it must be called with None as the argument, because there is no yield expression that could receive the value.
generator.throw(type[, value[, traceback]]) Raises an exception of type type at the point where generator was paused, and returns the next value yielded by the generator function. If the generator exits without yielding another value, a StopIteration exception is raised. If the generator function does not catch the passed-in exception, or raises a different exception, then that exception propagates to the caller.
generator.close() Raises a GeneratorExit at the point where the generator function was paused. If the generator function then raises StopIteration (by exiting normally, or due to already being closed) or GeneratorExit (by not catching the exception), close returns to its caller. If the generator yields a value, a RuntimeError is raised. If the generator raises any other exception, it is propagated to the caller. close() does nothing if the generator has already exited due to an exception or normal exit.
generator是由yield产生的,每次yield产生一个generator的元素
def gen(n): for i in range(n): yield ia=gen(5)print a.next()#0generator.send(value)是在当期那位置插入值
比如a.send(6)这时a中元素为0 6 1 2 3 4
generator.throw(type[, value[, traceback]]) Raises an exception of type type,跟raise用法差不多a.throw(StopIteration,'stop',)generator.close() :
def gen(n): for i in range(n): yield ia=gen(2)print a.next()a.close()print a.next()#StopIteration
- python iterator generator yield
- python语法31[iterator和generator+yield]
- 说说Python中的iterator,yield表达式及generator,coroutine
- Python生成器详解(从Iterable,Iterator知Generator,Yield)
- python generator / yield statement
- Python yield expression (generator)
- python中yield generator
- Python yield and generator
- python yield和generator
- python yield generator
- python yield generator 详解
- python yield generator 详解
- python yield generator 详解
- python yield generator 详解
- Python Yield Generator 详解
- python的迭代器iterator和生成器generator(关键字yield)的简单理解
- Python iterator, generator
- Python-iterator and generator
- java.lang.IncompatibleClassChangeError: class org.springframework.core.type.classreading.ClassMetada
- 1.反射的相关概念图:
- Github安装到windows7的配置说明
- C++ MyClass Date
- AndroidStudio2.0搭建Ndk环境&成功编译使用
- python iterator generator yield
- 设计模式 学习 4:
- 172. Factorial Trailing Zeroes
- 微信第三方授权流程:获取公众信息
- 实现微信布局的四种方式(二)
- zkw神树优化dijkstra
- jquery.qrcode.js生成二维码插件&转成图片格式
- 《Javascript秘密花园》学习笔记(终)
- Http中的重定向和转发