[Python-ideas] Add pop_callback to deque signature

Nick Coghlan ncoghlan at gmail.com
Wed Oct 4 08:48:19 EDT 2017

On 4 October 2017 at 15:47, Serhiy Storchaka <storchaka at gmail.com> wrote:
> 04.10.17 07:58, Nick Coghlan пише:
>> For deque specifically, I like Steven D'Aprano's suggestion of a
>> "__dropped__" or "__discard__" subclassing API that makes it
>> straightforward to change the way that queue overruns are handled
>> (especially if raising an exception from the new subclass method can
>> prevent the collection modification entirely - that way you could
>> readily change the deque semantics in a subclass such that if the
>> queue fills up, submitters start getting errors instead of silently
>> discarding older messages, allowing backpressure to be more easily
>> propagated through a system of queues).
> Wouldn't this harm performance? Looking up the attribute of the type is more
> costly than pushing/popping the item in the deque.

Aye, that would need to be considered, and may push the API towards
callback registration on the instance over using a subclassing API.
The performance considerations in the dict case are different, since
the default behaviour is to raise KeyError, where the cost of checking
for the method doesn't matter much either because the command is about
to terminate, or else because it is still much faster than
instantiating the caught exception.

However, most of the performance impact could also be avoided through
a PyCheck_Exact that only checks for the method for subclasses, and
not for regular deque instances.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list