[Python-Dev] Bug or not? Different behaviour iterating list and collections.deque (original) (raw)
Christos Georgiou tzot at mediconsa.com
Sun Jan 7 11:38:04 CET 2007
- Previous message: [Python-Dev] features i'd like [Python 3000] ... #3: fix super()
- Next message: [Python-Dev] Bug or not? Different behaviour iterating list andcollections.deque
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 [item20+1, item30+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:
processlist(numbers, list, False) got 1 got 2 got 61 got 91
List works as expected.
processlist(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:
processlist(numbers, collections.deque, True) got 1 got 2 Traceback (most recent call last): File "", line 1, in 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.
- Previous message: [Python-Dev] features i'd like [Python 3000] ... #3: fix super()
- Next message: [Python-Dev] Bug or not? Different behaviour iterating list andcollections.deque
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]