Possible Duplicate:
The Python yield keyword explained
Okay, I've probably phrased the question badly but this is the situation I have.
I have this line of code in Python 2.7 which I'm trying to understand:
yield (padding_zeros + number_string).encode("ascii")
In this line of code, padding_zeros
is a string of a variable number of '0's and number_string
is a number in the form of a string which can be any number between 0 to, say 10000.
I'm pretty confident that the .encode("ascii")
just converts the output of yield to ascii.
What I'm completely at sea about is what the yield (padding_zeros + number_string)
does.
I know it initiates a generator but I've spent a lot of time searching online and reading up on the syntax but I still can't work out what the generator actually does. It doesn't help that this is my first time looking at python (my ultimate aim is to convert this code to C#).
So, basically, please could someone explain to me what this line of code does? Does it just add the two strings together or does it do something a bit more complicated?
For further context, this is the block that that line of code appears in:
for current_length in range(4, max_length + 1):
for i in range(0, pow(10, current_length)):
number_string = str(i)
padding_zeros = "0" * (current_length - len(number_string))
yield (padding_zeros + number_string).encode("ascii")
(max_length
being exactly what it sounds like - a number indicating the maximum length of something)
Thanks in advance for any and all answers (even if they're telling me not to be such a fricking noob) :)
EDIT: Thanks very much for the answers - even though I could only pick one as the best answe they were all very helpful. And thanks for the comments as well - as some of them pointed out, What does the "yield" keyword do in Python? is a very good general guide to yield, generators and iterations even if I didn't find it an answer to my specific situation :)