![]() |
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):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. |
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()::
1As for the last two lines, it might make more sense if you understand that: :
x[:i] + x[i + 1:]:
permutations(x[:i] + x[i + 1:]):
yield [x[i]] + p |
Thanks, Arevos. I understand it now.
|
Neat generator example there, Arevos!
|
| All times are GMT -5. The time now is 1:35 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC