Link to module Stack
Dave Angel
davea at ieee.org
Sat Jan 9 05:56:36 EST 2010
Steven D'Aprano wrote:
> On Sat, 09 Jan 2010 01:07:39 -0800, kzagradskiy wrote:
>
>
>> class Stack:
>> def __init__(self):
>> self.__heap = []
>>
>
> A "heap" has a technical meaning in programming. To describe the
> internals of a stack as "heap" will be disconcerting and confusing to
> anyone who knows about stacks and heaps.
>
>
>
>> def push (self, word):
>> self.__heap.append (word)
>> def pop (self):
>> if len(self.__heap) == 0:
>> raise InnerInterpreterError, "stack underflow"
>>
>
> "InnerInterpreterError" is the most inappropriate exception name I've
> ever seen. It has nothing to do with the interpreter, it's a stack error.
>
>
It has everything to do with the (Forth) interpreter. Exceptions can
readily be named according to their application -- it's not always about
Python. Anyway, Forth has an inner-interpreter and an
outer-interpreter, and the name will make sense to a Forth programmer.
>> result = self.__heap[-1]
>> del self.__heap[-1]
>>
>
> That is better written as result = self.__heap.pop().
>
>
>
or even better, without the extra local var:
def pop (self):
if len(self.__heap) == 0:
raise InnerInterpreterError, "stack underflow"
return self.__heap.pop(1)
P.S. - I'm puzzled why the OP even put this message here. There's no
question posted with it.
DaveA
More information about the Python-list
mailing list