如果我与接口的类:防止修改而迭代
class AnIteratable(object):
def __init__(self):
#initialize data structure
def add(self, obj):
# add object to data structure
def __iter__(self):
#return the iterator
def next(self):
# return next object
...我将如何设置的东西,所以如果add()
被称为中间迭代的异常thown,类似于:
In [14]: foo = {'a': 1}
In [15]: for k in foo:
....: foo[k + k] = 'ohnoes'
....:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-15-2e1d338a456b> in <module>()
----> 1 for k in foo:
2 foo[k + k] = 'ohnoes'
3
RuntimeError: dictionary changed size during iteration
更新: 如果接口需要更多的方法,随意添加他们。我也删除了执行__iter__()
。
更新#2 基于kindall的回答,我嘲笑了下面的伪代码实现。请注意,_datastruture和索引到它的相关方法是抽象类,类作者必须编写他/她自己的数据结构遍历和位置指针机制。
class AnIteratable(object):
def __init__(self):
self._itercount = 0
self._datastructure = init_data_structure() #@UndefinedVariable
# _datastructure, and the methods called on it, are abstractions.
def add(self, obj):
if self._itercount:
raise RuntimeError('Attempt to change object while iterating')
# add object to data structure
def __iter__(self):
self._itercount += 1
return self.AnIterator(self)
class AnIterator(object):
def __init__(self, aniterable):
self._iterable = aniterable
self._currentIndex = -1 #abstraction
self._notExhausted = True
def next(self):
if self._iterable._datastructure.hasNext(self._currentIndex):
self._currentIndex += 1
return self._iterable._datastructure.next(self._currentIndex)
else:
if self._notExhausted:
self._iterable._itercount -= 1
self._notExhausted = False
raise StopIteration
def __next__(self):
return self.next()
# will be called when there are no more references to this object
def __del__(self):
if self._notExhausted:
self._iterable._itercount -= 1
更新3 阅读一些后,似乎__del__
可能不是正确的道路要走。以下可能是一个更好的解决方案,但它需要用户明确地发布一个未耗尽的迭代器。
def next(self):
if self._notExhausted and
self._iterable._datastructure.hasNext(self._currentIndex):
#same as above from here
def discard(self):
if self._notExhausted:
self._ostore._itercount -= 1
self._notExhausted = False
你是如何实现'next'的? –
如果'foo'是一个'dict',你可以在'foo.keys()[:]'中使用'for k。在其他情况下,它取决于'next'方法的实现。 –
@DavidRobinson我不是 - 这只是一个界面。我想知道如何为具有任意数据结构的任意类做这件事,因为我认为这种情况相当普遍。 – elhefe