Skip to content
Advertisement

Python deque maxlen does not show

I have

from collections import deque

dq = deque(range(10), maxlen =  10)
dq

dq.extendleft([10, 20, 30, 40])
dq

result

deque([40, 30, 20, 10, 3, 4, 5, 6, 7, 8])

enter image description here

but in book Fluent Python (2019), I see maxlen, like this

deque([40, 30, 20, 10, 3, 4, 5, 6, 7, 8], maxlen=10)

enter image description here

Is the different cause by version different?

Advertisement

Answer

This seems to be caused by IPython. If you do print(dq) or print(repr(dq)), you get your expected output, and same in a normal REPL.

In [1]: from collections import deque

In [2]: dq = deque(range(10), maxlen=10)

In [3]: dq
Out[3]: deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [4]: print(dq)
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxlen=10)

In [5]: print(repr(dq))
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxlen=10)
>>> from collections import deque
>>> dq = deque(range(10), maxlen=10)
>>> dq
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxlen=10)

Update: I’ve submitted a PR to IPython to fix the problem.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement