[Python-Dev] Bug or not? Different behaviour iterating list and collections.deque
Christos Georgiou
tzot at mediconsa.com
Sun Jan 7 11:38:04 CET 2007
Hello, people. I am not sure whether this is a bug or intentional, so I
thought checking it with you before opening a bug. I will explain this
issue, but please understand this is not a question for help to change the
algorithm (this has been done already), so it's not a question of c.l.py.
It's a matter of discrepancy.
A list that changes while iterating produces no errors, while a deque fails.
Given the following example code:
#code start
import itertools, collections
def item_is_special(item):
"Just a dummy check in this example"
return item % 3 == 0
def item_products(item):
"Also a dummy function for the example"
return [item*20+1, item*30+1]
def process_list(items, type_of_que, special_case):
# we want to process some items, but some of them
# produce other items to be processed by the
# same algorithm
products= type_of_que()
if special_case: products.append(-1)
for item in itertools.chain(items, products):
if item_is_special(item):
for product in item_products(item):
products.append(product)
else:
print "got", item
#code end
we have the following cases:
>>> process_list(numbers, list, False)
got 1
got 2
got 61
got 91
List works as expected.
>>> process_list(numbers, collections.deque, False)
got 1
got 2
deque does not work, most probably because deque.__iter__ of an empty deque
ignores later changes. For this reason the `special_case' parameter was
inserted in the code above, so that there is at least an item when
itertools.chain calls iter on the deque:
>>> process_list(numbers, collections.deque, True)
got 1
got 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "testdequeiter.py", line 17, in process_list
for item in itertools.chain(items, products):
RuntimeError: deque mutated during iteration
Is that intentional? If not, let me know so that I open a bug.
More information about the Python-Dev
mailing list