By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,112 Members | 1,601 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,112 IT Pros & Developers. It's quick & easy.

Recursion in compile()

P: n/a
Hi,
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)

I did a search on 'recursion' in comp.lang.python but didn't turn up
anything relevant. Maybe I've highhhhhh expectations :-)
-- Naren.
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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?

--
Steven Taschuk st******@telusplanet.net
"I tried to be pleasant and accommodating, but my head
began to hurt from his banality." -- _Seven_ (1996)

Jul 18 '05 #2

P: n/a
On Wed, 16 Jul 2003 20:13:24 -0600, Steven Taschuk <st******@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')
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?


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
... else:
... return n * fact(n-1)
...
... print fact(12)
... """ cobj = compile(src_code, 'myfile', 'exec')
eval(cobj)

479001600

(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).

Regards,
Bengt Richter
Jul 18 '05 #3

P: n/a
Steven Taschuk <st******@telusplanet.net> wrote in message news:<ma**********************************@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?)
Jul 18 '05 #4

P: n/a
Quoth Narendra C. Tulpule:
[...]
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?)


That would do it -- with a string argument, eval only does
expressions, not statements (and so in particular, not def
statements).

Try
compile('exec src_code')
instead.

--
Steven Taschuk st******@telusplanet.net
Every public frenzy produces legislation purporting to address it.
(Kinsley's Law)

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.