Re: recursive internal function

Guido.van.Rossum@cwi.nl
Sat, 30 Oct 1993 01:28:55 +0100

> Next question: how do I write something like the function foo():
>
> def bar (x):
>
> def foo (y):
>
> if (y > 100): return
> ... do something with y...
> foo (y*2)
>
> ... do something with x...
> foo(x*3)
>
> bar(1)
>
> I suppose I just can't.

Well... You could add "global foo" before the "def foo...", but I
suppose it would defeat the purpose of making foo a local function.

Alternatively, you could define and instantiate a local class with a
recursive method foo(). But by the time you're doing that, you may be
better off structuring your whole program as a class instead of as a
module with functions...

The moral of this story? You can trade globals for classes. Or
perhaps: don't try to write Pascal in Python ;-)

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>