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

gh-153357: Add object-oriented MinHeap and MaxHeap classes to heapq#153821
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

Documentation build overview

📚 cpython-previews | 🛠️ Build #33624913 | 📁 Comparing 4d1581a against main (f62050d)

  🔍 Preview build  

3 files changed
± library/datatypes.html
± library/heapq.html
± whatsnew/3.16.html

@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

@picnixz yes, sorry, I converted the PR to draft; I just proposed it so it serves as reference in the conversation, as some people is pushing for having this.

Also fixed the issue number. Thanks!

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