Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 7th, 2006, 6:37 PM   #1
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 837
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Recursion: yield vs. return

I have been learning Python for a while now. I was browsing old posts in this forum and stumbled across this thread in which Arevos posted the following code:

def permutations(x):
    if len(x) <= 1:
        yield x
    else:
        for i in range(len(x)):
            for p in permutations(x[:i] + x[i + 1:]):
                yield [x[i]] + p
I understand recursion, but what is yield and how does it differ from return? And what is a recursive generator?

Also, I understand the gist of how this function works, but it is more complicated than most recursive functions I have studied. Could someone please explain how this particular function works, especially the last two lines? Thanks.
titaniumdecoy is offline   Reply With Quote
Old Jun 8th, 2006, 2:57 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
The yield keyword turns a function into a generator. This allows a function to return more than once, and is typically used in for loops. For example:
def count_to_four():
    yield 1
    yield 2
    yield 3
    yield 4

for number in count_to_four():
    print number
The output when this is executed:
1
2
3
4
A recursive generator is simply a recursive function that uses yield instead of return.

As for the last two lines, it might make more sense if you understand that:
x[:i] + x[i + 1:]
Is the list x with the ith element removed. And therefore:
permutations(x[:i] + x[i + 1:])
Generates all permutation for the list x without the ith element. Then this line:
yield [x[i]] + p
Adds the ith element back onto the beginning of each permutation. In this way, it recursively generates permutations of the original list.
Arevos is offline   Reply With Quote
Old Jun 8th, 2006, 9:48 AM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 837
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Thanks, Arevos. I understand it now.
titaniumdecoy is offline   Reply With Quote
Old Jun 8th, 2006, 10:52 PM   #4
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Thumbs up

Neat generator example there, Arevos!
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:12 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC