Skip to content

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

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

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!

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Oct 18, 2026
@rhettinger

Copy link
Copy Markdown
Contributor

I also thought that we long ago decided not to do this.

The heaps really don't need an object oriented wrapper.

@ambv

ambv commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

This PR is attached to gh-153357, which was closed as a duplicate of earlier heapq discussion in #62034 and #143326. Closing to cull the number of open pull requests. Do not reopen this pull request without getting a go-ahead from a triager or core developer on the canonical issue first.

@ambv ambv closed this Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants