Recursion in compile()
bokr at oz.net
Thu Jul 17 17:31:29 CEST 2003
On Wed, 16 Jul 2003 20:13:24 -0600, Steven Taschuk <staschuk at telusplanet.net> wrote:
>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')
>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?
It works for me too. But the OP is saying "recursion *in* compile()" [my emphasis and spelling],
and the recursion of fact doesn't involve recursion of compiler calls (at least not in the
fact code). Maybe he is after something else?
BTW, (to the OP), using triple quotes makes things more readable sometimes:
>>> src_code = """\
... def fact(n):
... if n <= 1:
... return 1
... return n * fact(n-1)
... print fact(12)
>>> cobj = compile(src_code, 'myfile', 'exec')
(For yet another readability improvement, leaving out the backslash would make
a blank line that's not in the OP's string, but wouldn't hurt the compilation).
More information about the Python-list