gh-153357: Add object-oriented MinHeap and MaxHeap classes to heapq by facundobatista · Pull Request #153821 · python/cpython · GitHub
Skip to content

gh-153357: Add object-oriented MinHeap and MaxHeap classes to heapq - #153821

Draft
facundobatista wants to merge 4 commits into
python:mainfrom
facundobatista:heapq-oo
Draft

facundobatista wants to merge 4 commits into
python:mainfrom
facundobatista:heapq-oo

Conversation

@facundobatista

@facundobatista facundobatista commented Jul 16, 2026

Copy link
Copy Markdown
Member

Add heapq.MinHeap and heapq.MaxHeap, an object-oriented interface to the existing heapq functions.

Both classes wrap a list and expose the heap operations as methods, so you no longer have to pass the list around to the module-level functions:

>>> from heapq import MinHeap
>>> heap = MinHeap([3, 1, 2])
>>> heap.push(0)
>>> heap.pop()
0
>>> len(heap)
3

The classes provide push, pop, pushpop, replace, nsmallest and nlargest, plus __len__, __bool__ and __repr__. MaxHeap behaves as a max-heap, using the *_max variants under the hood.

The implementation is pure Python and delegates to the C-accelerated module functions, so there is no performance regression.

Includes documentation, a What's New entry and tests.

@read-the-docs-community

Copy link
Copy Markdown

@picnixz

picnixz commented Jul 17, 2026

Copy link
Copy Markdown
Member

I thought we decieed not to do it? the linked issue is also the wrong one

@facundobatista
facundobatista marked this pull request as draft July 17, 2026 14:32
@facundobatista facundobatista changed the title gh-137200: Add object-oriented MinHeap and MaxHeap classes to heapq gh-153357: Add object-oriented MinHeap and MaxHeap classes to heapq Jul 17, 2026
@facundobatista

Copy link
Copy Markdown
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants