首页 > 解决方案 > 如何从 __next__ 方法产生?

问题描述

我认为以下代码将表达所需的结果:

class LockIterator(object):

    def __init__(self, lock_list):
        self.lock_list = lock_list

    def __iter__(self):
        return self

    def __next__(self):

        for resource in self.lock_list:
            print( "Locking N resources" )

            yield resource
            print( "Unlocking N resources" )

        print( "Unlocking remaining resources" )
        raise StopIteration


for item in LockIterator( ["Resource 1", "Resource 2", "Resource 3"] ):
    print("Safe resource usage:", item)

但是,在 Python 上运行它,我得到了一个无限循环:

Safe resource usage: <generator object LockIterator.__next__ at 0x000001A8BDA24938>
Safe resource usage: <generator object LockIterator.__next__ at 0x000001A8BB8AEE60>
Safe resource usage: <generator object LockIterator.__next__ at 0x000001A8BDA24938>
Safe resource usage: <generator object LockIterator.__next__ at 0x000001A8BB8AEE60>
Safe resource usage: <generator object LockIterator.__next__ at 0x000001A8BDA24938>
Safe resource usage: <generator object LockIterator.__next__ at 0x000001A8BB8AEE60>
Safe resource usage: <generator object LockIterator.__next__ at 0x000001A8BDA24938>
Safe resource usage: <generator object LockIterator.__next__ at 0x000001A8BB8AEE60>
...

在我的想象中,它应该是这样运行的:

Locking N resources
Safe resource usage: Resource 1
Unlocking N resources
Locking N resources
Safe resource usage: Resource 2
Unlocking N resources
Locking N resources
Safe resource usage: Resource 3
Unlocking N resources
Unlocking remaining resources

您知道如何在普通的 for 循环中自动强制执行此行为吗?

for item in LockIterator( ["Resource 1", "Resource 2", "Resource 3"] ):
    print("Safe resource usage:", item)

标签: python-3.xfor-loopiteratorgeneratoryield

解决方案


我还设法通过删除__next__并将其主体移动到__iter__方法来修复它:

class LockIterator(object):

    def __init__(self, lock_list):
        self.lock_list = lock_list

    def __iter__(self):

        for resource in self.lock_list:
            print( "Locking N resources" )

            yield resource
            print( "Unlocking N resources" )

        print( "Unlocking remaining resources" )

for item in LockIterator( ["Resource 1", "Resource 2", "Resource 3"] ):
    print("Safe resource usage:", item)

推荐阅读