Python-based iterators and generators

Source: Internet
Author: User

作用:不依靠索引遍历,可遍历无索引对象特性:分批次取值,比有索引对象更节省内存缺点:无法获取迭代器的长度    使用不如列表索引灵活    一次性的,只能从头到尾取值判断:只要本身有__iter__方法,就是可迭代的try :               #异常退出,for 循环自带检测    dadadaexcept StopIteration:    break
简单定义:生成器就是一个函数 包含 yield 语句特性:分步运行函数,能够保存函数当前状态与return的区别 :return 只能返回一次,yield可以返回多次值
把函数变成生成器(生成器本身也是迭代器)用yield 可以返回多次函数在暂停以及下一次继续下一次运行状态有yield保存

Python-based iterators and generators

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.