Recursion in compile()

Narendra C. Tulpule naren at trillium.com
Thu Jul 17 14:37:11 EDT 2003


Steven Taschuk <staschuk at telusplanet.net> wrote in message news:<mailman.1058409301.23691.python-list at python.org>...
> Quoth Narendra C. Tulpule:
> >   is there any way to allow recusrion in compile()? Something like:
> > 
> > src_code = 'def fact(n):\n\tif n <= 1:\n\t\treturn 1\n\telse:' + \
> >            '\n\t\treturn n * fact(n-1)\n\nprint fact(12)\n'
> > cobj = compile(src_code, 'myfile', 'exec')
> > eval(cobj)
> 
> This works fine for me -- it prints 479001600 as expected, on both
> 2.2.2 and 2.3b1.  What is the problem you're seeing?

Sorry, you're right, it does work for me.
I was trying something like 
cobj = compile('eval(src_code)', ...)
eval(cobj)
And that complains about syntax error (because I am trying to define a
function within eval string?)




More information about the Python-list mailing list