473,385 Members | 1,409 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,385 software developers and data experts.

Recursion and Generators

I'm teaching myself Python and never having used recursion and
generators, created the simple recursive program shown below and
single stepped through it in IDLE with the debugger turned on to get a
feel for how recursion and generators worked.

def testRecursion(n):
if n == 1:
yield [1]
else:
for result in testRecursion(n-1):
yield result + [n]

print list(testRecursion(4))

As I expect, the program keeps calling testRecursion recursively, and
then backs its way up, yielding a new result each time it exits. All
well and good so far.

However, it then calls testRecursion recursively a second time (see
the sequence of calls below), and of course no results are yielded
this time around.

I'm not sure why it does so. Is it trying to force each genererator to
"drop off the end" and return a StopIteration? If so, should it not
discover that it is done with a level as soon as it backs up to the
next higher level?
'__main__'.testRecursion(), line 5: for result in testRecursion(n-1):
'__main__'.testRecursion(), line 2: if n == 1:
'__main__'.testRecursion(), line 5: for result in testRecursion(n-1):
'__main__'.testRecursion(), line 2: if n == 1:
'__main__'.testRecursion(), line 5: for result in testRecursion(n-1):
'__main__'.testRecursion(), line 2: if n == 1:
'__main__'.testRecursion(), line 3: yield[1]
'__main__'.testRecursion(), line 3: yield result + [n]
'__main__'.testRecursion(), line 3: yield result + [n]
'__main__'.testRecursion(), line 3: yield result + [n]
'__main__'.testRecursion(), line 5: for result in testRecursion(n-1):
'__main__'.testRecursion(), line 5: for result in testRecursion(n-1):
'__main__'.testRecursion(), line 5: for result in testRecursion(n-1):
Sincerely

Thomas Philips
Jul 18 '05 #1
3 1531

"Thomas Philips" <tk****@hotmail.com> wrote in message
news:b4**************************@posting.google.c om...
I'm teaching myself Python and never having used recursion and
generators, created the simple recursive program shown below and
single stepped through it in IDLE with the debugger turned on to get a
feel for how recursion and generators worked.
Bold project. I learned each separately. The two together are sometimes a
challenge to understand.
def testRecursion(n):
if n == 1:
yield [1]
else:
for result in testRecursion(n-1):
yield result + [n]

print list(testRecursion(4))

As I expect, the program keeps calling testRecursion recursively, and
then backs its way up, yielding a new result each time it exits. All
well and good so far.

However, it then calls testRecursion recursively a second time (see
the sequence of calls below), and of course no results are yielded
this time around.

I'm not sure why it does so. Is it trying to force each genererator to
"drop off the end" and return a StopIteration? If so, should it not
discover that it is done with a level as soon as it backs up to the
next higher level?
'__main__'.testRecursion(), line 5: for result in testRecursion(n-1):
'__main__'.testRecursion(), line 2: if n == 1:
'__main__'.testRecursion(), line 5: for result in testRecursion(n-1):
'__main__'.testRecursion(), line 2: if n == 1:
'__main__'.testRecursion(), line 5: for result in testRecursion(n-1):
'__main__'.testRecursion(), line 2: if n == 1:
'__main__'.testRecursion(), line 3: yield[1]
'__main__'.testRecursion(), line 3: yield result + [n]
'__main__'.testRecursion(), line 3: yield result + [n]
'__main__'.testRecursion(), line 3: yield result + [n]
'__main__'.testRecursion(), line 5: for result in testRecursion(n-1):
'__main__'.testRecursion(), line 5: for result in testRecursion(n-1):
'__main__'.testRecursion(), line 5: for result in testRecursion(n-1):


Since the yield result line is line 6, not 3, and definitely after rather
than before the line 5 for loop, I do not understand this output. Can you
clarify?

Terry J. Reedy

Jul 18 '05 #2
Terry,

My mistake: I typed the lines as I could not copy them from IDLE's
debug window. All yields other than the first one should indeed be
line 6.

Thomas
Jul 18 '05 #3
> def testRecursion(n):
if n == 1:
yield [1]
else:
for result in testRecursion(n-1):
yield result + [n]

print list(testRecursion(4))


Is this what you were trying to do?
def testRecursion(n): .... if n==1:
.... yield 1
.... else:
.... for x in testRecursion(n-1): yield x
.... yield n
.... list(testRecursion(1)) [1] list(testRecursion(2)) [1, 2] list(testRecursion(3)) [1, 2, 3] list(testRecursion(4))

[1, 2, 3, 4]

The generator should yield individual values one after
another, unlike a list of all values like in your example.

HTH,
-param
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Francis Avila | last post by:
Below is an implementation a 'flattening' recursive generator (take a nested iterator and remove all its nesting). Is this possibly general and useful enough to be included in itertools? (I know...
9
by: Francis Avila | last post by:
A little annoyed one day that I couldn't use the statefulness of generators as "resumable functions", I came across Hettinger's PEP 288 (http://www.python.org/peps/pep-0288.html, still listed as...
10
by: Christopher T King | last post by:
Is it feasable, and/or desirable to have Python optimize tail-recursive calls, similar to Scheme and gcc -O2? i.e. effectively compile this: def foo(n): return foo(n-1) into this: def...
3
by: Carlos Ribeiro | last post by:
As a side track of my latest investigations, I began to rely heavily on generators for some stuff where I would previsouly use a more conventional approach. Whenever I need to process a list, I'm...
3
by: Michael Sparks | last post by:
Hi, I'm posting a link to this since I hope it's of interest to people here :) I've written up the talk I gave at ACCU Python UK on the Kamaelia Framework, and it's been published as a BBC...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
24
by: proctor | last post by:
hello, i have a small function which mimics binary counting. it runs fine as long as the input is not too long, but if i give it input longer than 8 characters it gives RuntimeError: maximum...
13
by: Martin Sand Christensen | last post by:
Hi! First a bit of context. Yesterday I spent a lot of time debugging the following method in a rather slim database abstraction layer we've developed: ,---- | def selectColumn(self,...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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.