← index #5244Issue #4612
Related · high · value 2.299
QUERY · ISSUE

collections.deque constructor does not take keyword arguments

openby nevercastopened 2019-10-21updated 2019-11-15
enhancementpy-core

I would like to construct a deque as follows collections.deque(maxlen=10). For this to work, iterables must have a default. The default should be an empty tuple or None, see: https://docs.python.org/3/library/collections.html#collections.deque

maxlen should also support keyword arguments but would not require a default, i.e not optional, since I don't need arbitrarily (unbounded) length deques.

CANDIDATE · ISSUE

ucollections.deque constructor should accept empty list

closedby goatchurchprimeopened 2019-03-17updated 2026-03-05

I spent some hours on the trying to get the deque constructor to work, and didn't sort it out until I searched this issues list and found some example code that was correct.

Even though the documentation says "iterable must be the empty tuple, and the new deque is created empty", I forgot that these words meant it had to be "()" and kept doing:

>>> from ucollections import deque
>>> q = deque([], 3)

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ValueError: 

I don't see why this has to be an error (it's not in CPython), or even why it's beneficial for the implementation to require the initializer to be empty.

I can see that the class is designed to be compatible with the CPython code without sacrificing performance, which is why it has been decided to restrict this to fixed-length deques and only two of the methods. (This means that Micropython code will always run under CPython, but not necessarily the other way round -- as long as you don't use the flags parameter.)

But I think the line has been drawn too close by not allowing empty lists as well as empty tuples in constructor.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied