简体   繁体   English

Python枚举:使用后是否释放枚举对象?

[英]Python Enumerate: Is it the enumerate object released after usage?

I recently discovered enumerate to make it easier to identify index and value. 我最近发现枚举可以更轻松地识别索引和值。 However, I noticed an odd usage. 但是,我注意到一个奇怪的用法。 If you declare an enumerate object and use it in any way, the data inside the object disappears afterward. 如果您声明枚举对象并以任何方式使用它,则对象内部的数据此后将消失。

What is happening here? 这是怎么回事

In a simple example, you declare an object with an enumerated list. 在一个简单的示例中,您声明一个带有枚举列表的对象。 You convert it back to a list once. 您一次将其转换回列表。 On the second try, it is empty. 第二次尝试时,它为空。

A = [1, 2, 3, 4]
enuObject = enumerate(A)
>>> list(enuObject)
[(0, 1), (1, 2), (2, 3), (3, 4)]
>>> list(enuObject)
[]

I noticed this when I declared an enumerate call outside of a loop vs in it. 当我在循环内或循环外声明枚举调用时,我注意到了这一点。

The enumerate object is an iterator, and will be released when the garbage collector finds it's no longer used. enumerate对象是一个迭代器,当垃圾收集器发现它不再使用时将被释放。 However, in this example, the iterator for the enumerate is being completely used by the list function. 但是,在此示例中, list函数完全使用了enumerate的迭代器。 What this does is very similar to: 这与以下内容非常相似:

items = []
for e in en:
    items.append(e)

return items

It takes every element from the enumeration object en , adds them to a list, and then returns the list when there aren't any items left. 它从枚举对象en获取每个元素,将它们添加到列表中,然后在没有剩余任何项目时返回列表。 So, if you call list again on the fully-consumed enumeration object, the for e in en won't have any items to add to the list, and so it'll just return an empty list. 因此,如果您再次在完全消耗的枚举对象上调用list ,则for e in enfor e in en将没有任何项目要添加到列表中,因此它将仅返回一个空列表。


Iterator 迭代器

An object representing a stream of data. 表示数据流的对象。 Repeated calls to the iterator's next() method return successive items in the stream. 重复调用迭代器的next()方法将返回流中的后续项。 When no more data are available a StopIteration exception is raised instead. 如果没有更多数据可用,则会引发StopIteration异常。 At this point, the iterator object is exhausted and any further calls to its next() method just raise StopIteration again. 此时,迭代器对象已用尽,对它的next()方法的任何进一步调用只会再次引发StopIteration。 Iterators are required to have an __iter__() method that returns the iterator object itself so every iterator is also iterable and may be used in most places where other iterables are accepted. 迭代器必须具有返回迭代器对象本身的__iter__()方法,因此每个迭代器也是可迭代的,并且可以在接受其他可迭代的大多数地方使用。 One notable exception is code which attempts multiple iteration passes. 一个值得注意的例外是尝试多次迭代遍历的代码。 A container object (such as a list) produces a fresh new iterator each time you pass it to the iter() function or use it in a for loop. 每当您将容器对象(例如列表)传递给iter()函数或在for循环中使用它时,都会产生一个新的新迭代器。 Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container. 使用迭代器尝试执行此操作将仅返回上一次迭代过程中使用的相同的耗尽迭代器对象,使其看起来像一个空容器。


This is in the Python document. 这在Python文档中。 So basically it is intentionally designed that way. 所以基本上它是有意设计的。

In short words, When you reach to the end of an iterator, StopIteration is being raised. 简而言之,当您到达迭代器的末尾时,将引发StopIteration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM