python list __iter__ 方法在每个循环上调用?
我正在尝试创建一个继承自 python 列表的类。我希望列表的元素在列表的每个循环中都被初始化/最终确定。我认为这可以通过重写 python 列表的 __iter__ 方法来完成,但我似乎无法让它工作。 __iter__
方法似乎只调用一次? (见下文)
class MyList(list):
def __iter__(self):
print 'do something'
return list.__iter__(self)
my_list = MyList(range(10))
print my_list
for item in my_list:
print item
输出
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
do something
0
1
2
3
4
5
6
7
8
9
知道如何实现我想要做的事情吗?
I am trying to make a class which inherits from a python list. I want the elements of the list to be initialized/finalized with every loop of the list. I thought this could be done by overriding the __iter__
method of the python list but I can't seem to get it to work. The __iter__
method appears to called only once ? (see below)
class MyList(list):
def __iter__(self):
print 'do something'
return list.__iter__(self)
my_list = MyList(range(10))
print my_list
for item in my_list:
print item
Output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
do something
0
1
2
3
4
5
6
7
8
9
Any idea how I can achieve what I want to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以简单地从 __iter__() 返回一个生成器表达式
ncoghlan 建议使用生成器代替生成器表达式,这样可以更轻松地进行调试,
或者您可以在此处使用 imap
You can simply return a generator expression from
__iter__()
ncoghlan suggests using a generator in place of the generator expression which makes for easier debugging
alternatively you could use imap here
__iter__
返回一个 迭代器对象。如果您需要在每次迭代中执行某些操作,则必须实现自己的(它必须实现链接文档中描述的两种方法)。__iter__
returns an iterator object. If you need to do something in every iteration, you have to implement your own (it has to implement two methods described in the linked docs).python类的
__iter__()
方法实际上返回一个迭代器对象。请参阅以下内容以供参考: http://docs.python.org/library/stdtypes .html#iterator-types
在迭代器对象上,循环的每一步都会调用
next()
方法。您可以编写一个自定义迭代器,该迭代器将由您的自定义列表返回。The python class
__iter__()
method actually returns an iterator object.See the following for reference: http://docs.python.org/library/stdtypes.html#iterator-types
On the iterator object the
next()
method will be called on each step of the loop. You could write a custom iterator which would be returned by your custom list.列表对此没有直接控制权。多个循环可以并行访问列表(例如在嵌套循环中)。每个循环都由迭代器控制,每次调用时,__iter__() 都应该返回迭代器。您不能始终返回相同的迭代器实例,因为所述并行循环将相互干扰。
但在迭代器内,您可以根据需要自由实例化项目。从迭代器内部完成它们并不明智,因为一旦返回一个项目,您就失去了对它的控制。如果您稍后完成它,仍然依赖于该项目的外部代码可能会失败。如果您需要完成您的商品,请使其成为商品消费者的责任。
A list has no direct control over this. Several loops can access a list in parallel (e.g. in nested loops). Each loop is controlled by an iterator, and
__iter__()
should return iterators, every time it's called. You can't return the same iterator instance all the time, because said parallel loops will then interfere with each other.But inside an iterator you're free to instantiate items as you need. Finalizing them from inside the iterator is hardly wise, because once you returned an item, you lost control over it. If you finalize it at a later moment, outer code that still relies on the item may fail. If you need to finalize your items, make it a responsibility of items' consumer.
我不明白你到底想做什么,但你已经被告知的是真的。
next()
是每次迭代中调用的方法。(我没有测试过)
I don't understand exactly what you are trying to do, but what you've already been told is true.
next()
is the method called in every iteration.(I didn't tested it)