python-3.x - 如何从 __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)
解决方案
我还设法通过删除__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)
推荐阅读
- php - Yii2 Migration 将数据移动到其他表
- php - 计数页面四舍五入 PHP
- android - android:包含回收器视图的片段中的复选框使用情况
- java - 这个 C# 泛型方法模式的等效 Java 实现是什么
- javascript - 刷新数据源自动完成jQuery
- python-2.7 - python 2.7中的CSV读取
- angular - Linting 错误:将此数组“排序”操作移至单独的语句
- android - Android studio 循环依赖
- unity3d - 在 Unity 3d 中检索多个 Set-Cookie 标头
- node.js - 如何安全地从 Electron 打开这个外部应用程序?