Python 3.x 如何从“下一步”方法中获得收益?

Python 3.x 如何从“下一步”方法中获得收益?,python-3.x,for-loop,iterator,generator,yield,Python 3.x,For Loop,Iterator,Generator,Yield,我认为下面的代码可以表达期望的结果: 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( "L

我认为下面的代码可以表达期望的结果:

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>
...
您知道如何在普通for循环中自动强制执行此行为吗

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

每当for循环需要新项时,都会调用
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。由于您的
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu>是一个生成器,因此每次都会返回这个生成器

相反,您可以去掉该类,只编写一个生成器:

def LockIterator(lock_list):
    # better name this lockify or something else in snake_case
    for resource in lock_list:
        print("Locking N resources")

        yield resource
        print("Unlocking N resources")

    print("Unlocking remaining resources")

每当for循环需要新项时,都会调用
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。由于您的
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

相反,您可以去掉该类,只编写一个生成器:

def LockIterator(lock_list):
    # better name this lockify or something else in snake_case
    for resource in lock_list:
        print("Locking N resources")

        yield resource
        print("Unlocking N resources")

    print("Unlocking remaining resources")

我还通过删除
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
并将其主体移动到
\uuuuuuuuuuuuuuuuuuuuuuuuu
方法来修复它:

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)

我还通过删除
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
并将其主体移动到
\uuuuuuuuuuuuuuuuuuuuuuuuu
方法来修复它:

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)