I have
JavaScript
x
8
1
from collections import deque
2
3
dq = deque(range(10), maxlen = 10)
4
dq
5
6
dq.extendleft([10, 20, 30, 40])
7
dq
8
result
JavaScript
1
2
1
deque([40, 30, 20, 10, 3, 4, 5, 6, 7, 8])
2
but in book Fluent Python (2019), I see maxlen, like this
JavaScript
1
2
1
deque([40, 30, 20, 10, 3, 4, 5, 6, 7, 8], maxlen=10)
2
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.
JavaScript
1
13
13
1
In [1]: from collections import deque
2
3
In [2]: dq = deque(range(10), maxlen=10)
4
5
In [3]: dq
6
Out[3]: deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
7
8
In [4]: print(dq)
9
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxlen=10)
10
11
In [5]: print(repr(dq))
12
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxlen=10)
13
JavaScript
1
5
1
>>> from collections import deque
2
>>> dq = deque(range(10), maxlen=10)
3
>>> dq
4
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxlen=10)
5
Update: I’ve submitted a PR to IPython to fix the problem.