[Python-ideas] Add pop_callback to deque signature
Steven D'Aprano
steve at pearwood.info
Tue Oct 3 07:04:46 EDT 2017
On Tue, Oct 03, 2017 at 10:18:13AM +0200, Michel Desmoulin wrote:
> My initial proposal would have been to add a data structure to allow
> something like deque, but allowing cheaper random accesses.
That would be a list :-)
Most data structures trade off performance in one area for performance
in another. Lists have good random access, but slow insertion and
deletion at the start. Deques have fast insertion and deletion at both
ends, but slower random access.
If it were possible to have a lightweight sequence data structure with
fast O(1) random access AND insertion/deletion at the same time, that
would be a candidate to replace both lists and deques in the stdlib. If
you have such a data structure in mind, you should propose it.
[...]
> So my more modest and realistic proposal would be to add a callback on
> deque signature:
>
> collections.deque(iterable, maxlen, pop_callback)
>
> pop_callback would accept any callable, and call it everytime an element
> is removed from the deque, allowing third party libraries to then do
> whatever they need with it.
Adding callbacks to basic data structures like this seems like a
hypergeneralisation. How often are they going to be used? Not often
enough to make it worthwhile, I think.
But if we did something like that, I think that following the design of
dicts would be a closer match to Pythonic style than a callback. Dicts
support a special __missing__ method in subclasses. Perhaps deque
could do something similar?
Subclass deque and give it a __dropped__ method, and whenever an item is
dropped from the deque the method will be called with the value dropped
(and perhaps a flag telling you which end it was dropped from).
To me, that sounds like a more Pythonic interface than a callback.
--
Steve
More information about the Python-ideas
mailing list