← index #5244PR #13
Off-topic · high · value 0.461
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 · PULL REQUEST

Add support of `len` to `collections.deque`

closedby nvbnopened 2014-12-14updated 2014-12-14

Currently collections.deque in micropython doesn't support len:

>>> from collections.deque import deque
>>> q = deque()
>>> len(q)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'deque' has no len()

But cpython supports:

In [1]: from collections import deque
In [2]: q = deque()
In [3]: len(q)
Out[3]: 0

So I've added it to micropython collections.deque.

3 comments
nvbn · 2014-12-14

I've also added __bool__ for supporting:

>>> d = deque()
>>> if d:
...     print('not empty')
... else:
...     print('empty')
... 
empty
>>> d.append(42)
>>> if d:
...     print('not empty')
... else:
...     print('empty')
... 
not empty
pfalcon · 2014-12-14

Thanks, merged! Minor nitpick: can you please follow existing commit message format "module: Description": (I rebase-reworded your messages):
https://github.com/micropython/micropython-lib/commits/master . I also "optimized" __bool__: https://github.com/micropython/micropython-lib/commit/df85a27af439ad66f99eb2b9a91359559d65a2e4 ;-). Pushed to PyPI as 0.1.1.

nvbn · 2014-12-14

Thanks, sure.

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