I was playing with standard library module [heapq](heapq — Heap queue algorithm — Python 3.13.2 documentation, which Imho has a very weird API where you use a list as a heap… I didn´t really like that and for what I see I’m not the only one.
Soon I realized that I could patch everything that I don´t like with just one extra line:
class heapq(list):
from heapq import heappush as hpush, heappop as hpop
Needless to say I was very proud of it because now I can use the heap just like a list, with his own type and methods, for me looks very elegant.
my_heap = heapq()
to_append = (1,"task1")
my_heap.push(to_append)
Buuut, I got:
TypeError: heappush expected 2 arguments got 1
Interesting because the definition of heappush is:
def heappush(heap, item):
"""Push item onto heap, maintaining the heap invariant."""
heap.append(item)
_siftdown(heap, 0, len(heap)-1)
So now ‘heap’ is the ‘self’ and therefore I’m passing both arguments 0.o, interesting…
then i realize these lines at the end of the library:
# If available, use C implementation
try:
from _heapq import *
except ImportError:
pass
I remove these and for my surprise everything works !!
But does this mean that my class don´t pass the instance if the method is coming from C? Is this a bug from python itself? anyone knows how does this work?