[Zope] Python method: recursive function gives NameError
Michel Pelletier
michel@digicool.com
Tue, 28 Nov 2000 10:42:18 -0800
Willem Broekema wrote:
>
> I have a simple recursive function in a Python method that gives a
> NameError (2.2.4 on Win98).
>
> The function (in PM named 'rec'):
> ---
> def myfunc(i):
> if i >= 10: return myfunc(1)
> else: return 1
>
> return myfunc(11)
> ---
>
> The end of the traceback:
> ---
> File c:\myzopeinstance\...cts\PythonMethod\PythonMethod.py, line 168, in
> __call__
> (Object: rec)
> (Info: ((), {}, None))
> File <string>, line 6, in rec
> (Object: myfunc)
> File <string>, line 3, in myfunc
> NameError: (see above)
> ---
>
> Isn't it allowed to have a recursive function here?
Yes, but that's not your problem. Your problem is it can't resolve the
name 'myfunc'. You need to say self.rec(1) if you are using old python
methods (and make sure self is a parameter), or self.myfunc(1) if you
are using new ones. Do you see why? The python corollary:
class Foo:
def myfunc(self, i):
if i >= 10: return self.myfunc(1)
else: return 1
-Michel