Summer reading list

Joe Cheng jmcheng at alum.mit.edu
Tue Aug 12 14:32:59 EDT 2003


> > 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)
> > item = heap.pop()
> > item = heap[0]
> > heapified = Heap(x)
> > item = heap.replace(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.)

Hmmm, I'm not sure if I buy this particular argument.  Line 5 in my
examples there was supposed to illustrate constructing a heap from a
list--so you could do pretty much the same thing.

list = [1, 19, 33, 40]
heap = Heap(list)

I hope you are not suggesting there is also something to be gained by
directly manipulating the list as a list after it has been heapified? :)

Oh, and of course a heap class should probably support a to_list()
export function (that returns a _copy_ of the internal list).

> 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
> can gain some benefits from this approach because it is exposing and
> operating on lists directly.

I've never heard this idea before... I'll need to chew on it a little.
Is this a common notion among Pythoners?

To me it sounds disturbingly like "Procedures are more flexible than
classes because you can compose classes out of procedures."  I'd worry
that the proliferation of procedures would undermine good OO, which
wants coupling at the class interface level.  Meanwhile you gain nothing
by using the raw procedures instead of dealing with the class.  (Haven't
thought too much about this, so I could be way off...?)

> This probably comes under "practicality beats purity".  (See 
> 'The Zen of
> Python', or type "import this" into your Python interpreter)

I don't consider myself an OO purist.  However, in practical terms, I'd
much rather work with a "black box" heap than one where I carry around
its state in a mutable data structure...






More information about the Python-list mailing list