473,378 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Recursion in compile()

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
4 1691
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
9
by: JustSomeGuy | last post by:
I have a class that looks something like this (Don't try to compile it, I haven't tested this) class KVP { string key; string value; list<KVP> sublist; };
11
by: Ken | last post by:
Hello, I have a recursive Sierpinski code here. The code is right and every line works fine by itself. I wish for all of them to call the function DrawSierpinski. But in this cae it only calls...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
0
by: Ray Wesley Kinserlow Jr. | last post by:
We have been studying tail recursion in my computer class. The prof told us that some compilers will turn a tail recursion into an iteration thus allowing many, many function calls to the...
6
by: Andre Kempe | last post by:
hej folks. i have a heap with fixed size and want to determine the depth of a element with given index at compile-time. therefore i wrote some templates. however, when i use template...
20
by: athar.mirchi | last post by:
..plz define it.
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.