Link to module Stack

Steve Holden steve at holdenweb.com
Sat Jan 9 21:31:18 CET 2010


Steven D'Aprano wrote:
> 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:

Since self.__heap is a list, the canonical Python for the above test
would, of course, be the much simpler

        if not self.__heap

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/




More information about the Python-list mailing list