New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support __add__, __mul__, and __imul__ for deques. #67981
Comments
One more step towards making the deque API a little closer to that for lists:
>>> deque('abc') + deque('def')
deque(['a', 'b', 'c', 'd', 'e', 'f']) |
Also incorporate __mul__ and __imul__. >>> d = deque('abc')
>>> d * 3
deque(['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'])
>>> d *= 2
>>> d
deque(['a', 'b', 'c', 'a', 'b', 'c']) |
The behavior for multiplying or adding doesn't seem quite so intuitive when you allow for a bounded deque. In particular, it doesn't seem obvious that multiplying when the deque is bounded, you'll get results like: >>> list(deque([1,2], 3) * 2)
[2, 1, 2] Similarly, when you support non-in-place addition, the bounded-ness of the result depends on the order of the values added. >>> list(deque([1,2], 3) + deque([1,2], 2))
[1, 2, 1]
>>> list(deque([1,2], 2) + deque([1,2], 3))
[1, 2] Not saying these are the wrong behaviors, but it's much more weird than what you get with other sequence types (since most sequence types aren't bounded), and possibly why deques didn't include these features in the first place; trying to act like a generalized sequence when you have features that don't fit the model is a titch odd. |
I think my first addition example is wrong (should produce [2, 1, 2]), but you get the idea. |
What would you want it to do? By design, the key feature of maxlen is pop old inputs to make way newer appends -- that is its essence. It would be surprising if the following invariant didn't hold: >>> deque('abc' * 3, maxlen=5) == deque('abc', maxlen=5) * 3
True That said, I don't expect that people are going to commonly be doing d*=n where len(d) > 1 and there is a maxlen > len(d)*n. The normal cases are unsurprising. |
I agree that popping old inputs is the normal way. What I'm trying to say is that it doesn't feel like "old" or "inputs" when you multiply. In that case (and maybe this is just me), it feels like a "reasonable" outcome could be to get an outcome similar to multiplying a list and then slicing the result to maxlen. I don't think it *should* do that, but either behavior feels weird, solely because sequence multiplication has never been applied to a bounded sequence before (to my knowledge), so the expectations aren't set. You end up with weird behaviors like len(seq) * 3 != len(seq * 3), which is a normal and expected outcome with every other sequence. If you know you're dealing with a bounded deque, it's less unexpected, but this is trying to make a deque a drop-in replacement for any other sequence when it doesn't behave like one. |
I don't see any technical reason not to proceed. Just because an unusual combination of parameters feels odd to you doesn't make it wrong. It is no different than (somelist * n)[-maxlen:]. |
New changeset b75160d24b7b by Raymond Hettinger in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: