This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: deque append and appendleft should return value if maxlen set
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: achampion, rhettinger
Priority: normal Keywords:

Created on 2015-09-20 08:36 by achampion, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg251154 - (view) Author: Alun Champion (achampion) Date: 2015-09-20 08:36
Currently if you append or appendleft when len(deque) == maxlen item from the other end of the deque is discarded. These should return the discarded value to allow you to write:

    x = deque.append(y)

vs

    if len(deque) == deque.maxlen():
        x = deque.pop()
    deque.append(y)

It should return None if len(deque) < maxlen or maxlen is not set.
msg251183 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-09-21 01:18
Sorry, Alun but I going to decline this feature request.  It has been considered before and that path wasn't taken for several reasons.  1) with the current api, it isn't hard to fetch the first value before appending, 2) in possible use cases such as a moving average computation it didn't make the code better (i.e. x==d[0]; d.append(y)), 3) list.append append is well known to alway return None (Guido considers that to be very informative about how the language works) so having deque.append return a value just looks weird to experienced Python programmers, and 4) it isn't entirely clear what the semantics should be if the deque hasn't yet reached its maximum length (it would be incorrect to assign None to x because that is a possible legitimate element of a deque and the alternative or raising an exception is also awkward, making it necessary to wrap append calls in a try/except).
History
Date User Action Args
2022-04-11 14:58:21adminsetgithub: 69379
2015-09-21 01:18:56rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg251183
2015-09-20 08:46:35serhiy.storchakasetassignee: rhettinger

type: enhancement
nosy: + rhettinger
versions: + Python 3.6
2015-09-20 08:36:22achampioncreate