python list __iter__ 方法在每个循环上调用?

发布于 2024-10-28 02:10:38 字数 486 浏览 3 评论 0原文

我正在尝试创建一个继承自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青瓷清茶倾城歌 2024-11-04 02:10:38

您可以简单地从 __iter__() 返回一个生成器表达式

class MyList(list):
    def __iter__(self):
        return (self.do_something(x) for x in list.__iter__(self))

    def do_something(self, x):
        print 'do something', x
        return x

my_list = MyList(range(10))
print my_list

for item in my_list:
    print item

ncoghlan 建议使用生成器代替生成器表达式,这样可以更轻松地进行调试,

class MyList(list):

    def __iter__(self):
        for x in list.__iter__(self):
            yield self.do_something(x)

    def do_something(self, x):
        print 'do something', x
        return x

my_list = MyList(range(10))
print my_list

for item in my_list:
    print item

或者您可以在此处使用 imap

from itertools import imap

class MyList(list):
    def __iter__(self):
        return imap(self.do_something, list.__iter__(self))

    def do_something(self, x):
        print 'do something', x
        return x


my_list = MyList(range(10))
print my_list

for item in my_list:
    print item

You can simply return a generator expression from __iter__()

class MyList(list):
    def __iter__(self):
        return (self.do_something(x) for x in list.__iter__(self))

    def do_something(self, x):
        print 'do something', x
        return x

my_list = MyList(range(10))
print my_list

for item in my_list:
    print item

ncoghlan suggests using a generator in place of the generator expression which makes for easier debugging

class MyList(list):

    def __iter__(self):
        for x in list.__iter__(self):
            yield self.do_something(x)

    def do_something(self, x):
        print 'do something', x
        return x

my_list = MyList(range(10))
print my_list

for item in my_list:
    print item

alternatively you could use imap here

from itertools import imap

class MyList(list):
    def __iter__(self):
        return imap(self.do_something, list.__iter__(self))

    def do_something(self, x):
        print 'do something', x
        return x


my_list = MyList(range(10))
print my_list

for item in my_list:
    print item
陌若浮生 2024-11-04 02:10:38

__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).

幻梦 2024-11-04 02:10:38

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.

对风讲故事 2024-11-04 02:10:38

我希望列表的元素在列表的每个循环中都被初始化/最终确定

列表对此没有直接控制权。多个循环可以并行访问列表(例如在嵌套循环中)。每个循环都由迭代器控制,每次调用时,__iter__() 都应该返回迭代器。您不能始终返回相同的迭代器实例,因为所述并行循环将相互干扰。

但在迭代器内,您可以根据需要自由实例化项目。从迭代器内部完成它们并不明智,因为一旦返回一个项目,您就失去了对它的控制。如果您稍后完成它,仍然依赖于该项目的外部代码可能会失败。如果您需要完成您的商品,请使其成为商品消费者的责任。

I want the elements of the list to be initialized/finalized with every loop of the list

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.

少钕鈤記 2024-11-04 02:10:38

我不明白你到底想做什么,但你已经被告知的是真的。 next() 是每次迭代中调用的方法。

class MyList(list):
    def next(self):
        print "Do something"
        return list.next(self)

(我没有测试过)

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.

class MyList(list):
    def next(self):
        print "Do something"
        return list.next(self)

(I didn't tested it)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文