[Python-Dev] Tail recursion

Michael Hudson mwh at python.net
Mon Nov 24 10:55:49 EST 2003

Guido van Rossum <guido at python.org> writes:

>> > Tail Recursion
>> > --------------
>> > from Me (my brain)
>> >
>> > Have proper tail recursion in Python.  Would require identifying where
>> > a direct function call is returned (could keep it simple and just do
>> > it where CALL_FUNCTION and RETURN bytecodes are in a row).  Also have
>> > to deal with exception catching since that requires the frame to stay
>> > alive to handle the exception.
>> >
>> > But getting it to work well could help with memory and
>> > performance. Don't know if it has been done for a language that had
>> > exception handling.
>> How is this different from stackless?
> AFAIK Stackless only curtails the *C* stack, not the chain of Python
> frames on the heap.

Oh, I see.  Yes.

> But I have a problem with tail recursion.  It's generally requested by
> new converts from the Scheme/Lisp or functional programming world, and
> it usually means they haven't figured out yet how to write code
> without using recursion for everything yet.  IOW I'm doubtful on how
> much of a difference it would make for real Python programs (which,
> simplifying a bit, tend to use loops instead of recursion).  And also
> note that even if an exception is not caught, you'd like to see all
> stack frames listed when the traceback is printed or when the debugger
> is invoked.

Well, this was why I assumed you didn't really want to do the full-on
tail-call-elimination thing :-)


  Need to Know is usually an interesting UK digest of things that
  happened last week or might happen next week. [...] This week,
  nothing happened, and we don't care.
                           -- NTK Now, 2000-12-29, http://www.ntk.net/

More information about the Python-Dev mailing list