栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

enumerate()是否产生一个生成器对象?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

enumerate()是否产生一个生成器对象?

虽然Python文档说这

enumerate
在功能上等同于:

def enumerate(sequence, start=0):    n = start    for elem in sequence:        yield n, elem        n += 1

enumerate
函数返回 迭代器 ,但不返回实际生成器。
help(x)
创建
enumerate
对象后调用可以看到以下内容:

>>> x = enumerate([1,2])>>> help(x)class enumerate(object) |  enumerate(iterable[, start]) -> iterator for index, value of iterable |   |  Return an enumerate object.  iterable must be another object that supports |  iteration.  The enumerate object yields pairs containing a count (from |  start, which defaults to zero) and a value yielded by the iterable argument. |  enumerate is useful for obtaining an indexed list: |      (0, seq[0]), (1, seq[1]), (2, seq[2]), ... |   |  Methods defined here: |   |  __getattribute__(...) |      x.__getattribute__('name') <==> x.name |   |  __iter__(...) |      x.__iter__() <==> iter(x) |   |  next(...) |      x.next() -> the next value, or raise StopIteration |   |  ---------------------------------------------------------------------- |  Data and other attributes defined here: |   |  __new__ = <built-in method __new__ of type object> |      T.__new__(S, ...) -> a new object with type S, a subtype of T

在Python中,生成器基本上是一种特定类型的迭代器,通过使用a

yield
从函数返回数据来实现。但是,
enumerate
实际上是用C而不是纯Python实现的,因此没有
yield
涉及。您可以在此处找到源:http
:
//hg.python.org/cpython/file/2.7/Objects/enumobject.c



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/645094.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号