Link to module Stack

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Jan 9 11:19:30 EST 2010


On Sat, 09 Jan 2010 05:56:36 -0500, Dave Angel wrote:

>> "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.

Pardon me, but I *am* a Forth programmer. Or was, it's been many years, 
and I'm rusty. I guess this is a difference of terminology: what you're 
calling an inner interpreter and an outer interpreter I know of as the 
Forth engine and the (text) interpreter. Gforth refers to them as such, 
so did Leo Brodie's Forth books, and the (ancient) Macintosh Forth 
compiler "Mach 2".

But in any case... a stack is an general-purpose data structure, and the 
error message shouldn't be coupled so tightly to one use. That would be 
like this (made-up) example:

>>> 1/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
GraphicsApplicationError: too few pixels to calculate average


Ridiculous, yes?


Yes, Forth uses a stack (technically two, a parameter stack and a return 
stack, and some implementations include a third, floating point, stack). 
Virtually all languages use stacks in their implementation, and Python 
byte-code is also stack-based.


>>>         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)

pop(1)? I don't think so.

>>> L = list('abcdef')
>>> L.pop(1)
'b'
>>> L
['a', 'c', 'd', 'e', 'f']


You probably meant pop(-1), but that's unnecessary because pop defaults 
to popping from the end of the list.



> P.S. - I'm puzzled why the OP even put this message here.  There's no
> question posted with it.

Me too.



-- 
Steven



More information about the Python-list mailing list