我有一个对象子类,它__ iter __
使用缓存生成器实现动态调度(我还有一个使 iter 缓存无效的方法),如下所示:
def __iter__(self):
print("iter called")
if self.__iter_cache is None:
iter_seen = {}
iter_cache = []
for name in self.__slots:
value = self.__slots[name]
iter_seen[name] = True
item = (name, value)
iter_cache.append(item)
yield item
for d in self.__dc_list:
for name, value in iter(d):
if name not in iter_seen:
iter_seen[name] = True
item = (name, value)
iter_cache.append(item)
yield item
self.__iter_cache = iter_cache
else:
print("iter cache hit")
for item in self.__iter_cache:
yield item
它似乎正在工作......有什么我可能不知道的陷阱吗?我在做一些可笑的事情吗?