If a deque is used with a fixed size, appending to one end will pop from the other end. However, there’s currently no way to reference this popped item. If I want to use it as a FIFO, I need to make sure that I pop from the right direction first, e.g.:
d = deque([],maxlen=1)
d.append(1)
old_value = d.popleft()
d.append(2)
This can be error prone if I accidentally pop from the wrong direction (though obviously not relevant in the n=1 scenario)
IMO this would be nicer if I could just do
d = deque([],maxlen=1)
d.append(1)
>> None
d.append(2)
>> 1
This would likely also imply
d = deque([],maxlen=3)
d.extend([1,2,3])
>> ()
d.extend([4,5,6])
>> (1,2,3)
Which seems fine to me? May even be useful to know the number of items popped during an operation, if queue length is unknown or something.
I considered making a PEP, but the guidelines said to socialize the idea first in this ideas forum, so I figured that might be a good idea
Thoughts? Anyone see a reason why this might be a bad idea to do?
Edit:
For additional context, the issue I’m facing is as follows:
I have a physical system where objects are traversing through. If an object is there, it needs to traverse through a number of steps, if it’s not there, we can skip a lot of stuff.
At every timepoint, we push in a new Widget, and pop out an old one. The machine start out being empty.
One part of the code would look something like this
queue = deque([], maxlen=5)
...
widget_entered = input_controller.widget_entered()
obj = Widget() if widget_entered else None
try:
old_obj = queue.popleft()
except IndexError:
pass
queue.append(obj)
if old_obj:
output_controller.process(old_obj)
Which could be rewritten to
queue = deque([], maxlen=5)
...
widget_entered = input_controller.widget_entered()
obj = Widget() if widget_entered else None
if old_obj:= queue.append(obj):
output_controller.process(old_obj)