tim at remove_if_not_spam.digitig.co.uk
Mon Sep 29 12:00:56 CEST 2003
On Mon, 29 Sep 2003 06:06:37 GMT, Dennis Lee Bieber
<wlfraed at ix.netcom.com> wrote:
> Each time Factorial makes a recursive call to itself (I know, that is
>redundant phrasing) it creates a new "block" of local variables, the
>variables in the previous block can not be seen. When a return
>statement is encountered, the block of locals is deleted, leaving the
>return value visible to the calling block.
I can't remember if the tutorial points it out, but for jakle's sake I
should mention that this means that the recursive implementation of
factorial uses a lot more memory than a simple loop implementation
(and is probably a lot slower too). Factorials are useful for
teaching recursion, but it's not usually the best way to implement a
factorial function . There are some applications that are not so
easy to implement as a loop, though (traversing a tree structure is
usually one of the first that nascent programmers encounter) and then
recursion starts being actually useful.
 Some languages depend on recursion much more than Python does, but
they will typically spot cases that can be converted to simple loops
and do that behind the scenes for efficiency.
More information about the Python-list