Summer reading list

John J. Lee jjl at pobox.com
Tue Aug 12 20:41:33 EDT 2003


Chad Netzer <cnetzer at sonic.net> writes:

> On Tue, 2003-08-12 at 08:56, Joe Cheng wrote:
> 
> > Quoting from the comments:
> > 
> > """Usage:
> > 
> > heap = []            # creates an empty heap
> > heappush(heap, item) # pushes a new item on the heap
[...]
> > It might just be my Java background creeping in (I'm a Python newbie), but,
> > wouldn't it be better if this was OO?
> > 
> > heap = Heap()
> > heap.push(item)
[...]
> > Otherwise the user could easily break the heap by doing something dumb to
> > the list...
> 
> True.  But the flexibility of using the builtin is also nice.  For
> example, you can add a bunch of objects to the list, then heapify once,
> rather than having to call heap.push() a bunch of times (which may be
> slower, because you need to maintain the heap property after you push
> each new item.)

I don't know what the design goals were, but perhaps there is benefit
in having heapq generic, rather than rigidly OO.  Certainly Numeric
was deliberately designed this way -- so ufuncs could be applied to
any old sequence, not just Numeric arrays (do I mean ufuncs?... it's
been a while since I used Numeric).


> I think the idea is that, if you want a real Heap class, you can build
> one very easily (see below).  And if you don't need a heap class, you

Certainly true, as Chad goes on to prove.


John




More information about the Python-list mailing list