On 12/02/2021 03:04, Terry Reedy wrote: > On 2/11/2021 3:22 PM, duncan smith wrote: > >> It seems that I can mutate a deque while iterating over it if I >> assign to an index, but not if I append to it. Is this the intended >> behaviour? > > Does the deque doc say anything about mutation while iterating? (Knowing > the author of deque, I would consider everything about it intentional > without *good* reason to think otherwise. > >>>>> from collections import deque >>>>> d = deque(range(8)) >>>>> it = iter(d) >>>>> next(it) >> 0 >>>>> d[1] = 78 > > This does not change the structure of the deque, so next does not > notice. It could be considered not be a mutation. It could be detected > by changing deque.__setitem__, but why bother and slow down all > __setitem__ calls. > >>>>> next(it) >> 78 >>>>> d.append(8) > > This changes the structure, which can apparently mess-up iteration. > >>>>> next(it) >> Traceback (most recent call last): >> File "<pyshell#650>", line 1, in <module> >> next(it) >> RuntimeError: deque mutated during iteration >>>>> > >
What I was really wondering was whether the behaviour is as it is because of the implementation or because it's how deques should ideally behave. i.e. In my implementation do I stick strictly to the same API, or allow it to differ? In some places I'm jumping through hoops to stick to the API, and (when it comes to iteration) I'm jumping through different hoops for different types of container (e.g. lists versus deques). BTW, the reason I am implementing these at all is that my containers are on-disk. Cheers. Duncan -- https://mail.python.org/mailman/listinfo/python-list