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 :)
OK, you know about generators, so the yield
part needs no explanation. Fine.
So what does that line actually do? Not very much:
It concatenates padding_zeros
and number_string
and then encodes the result to ASCII. Which in Python 2.7 is a no-op because the string is ASCII to begin with (it only consists of ASCII digits, by definition).
In Python 3, it would be different; here the .encode()
would have converted the string to a bytes
object. But in Python 2, it doesn't make any sense.
yield
is like return in a generator.
At the point that the yield
is executed, execution of the generator function stops, and the value is returned. The difference is that when the generator is invoked again, execution restarts at the yield statement, and continues until another yield is hit, or an (unhandled) exception is raised, or a return
is hit. The return
or exception will terminate the generator.
The point of a generator is that one can invoke it as x = next(generator)
or x = generator.next()
, and each time one will receive the value from the yield inside the generator. Generators are also iterable, so they may be used as the source of a loop: for x in generator: print x
.
Like in C#, the .
operator invokes the method named on its right on the object appearing on the operator's left. Accordingly, (padding_zeros + number_string).encode("ascii")
calls encode
on the result of (padding_zeros + number_string)
.
For the meaning of encode
, see here: http://docs.python.org/library/stdtypes.html#str.encode
For the language reference (assuming you are using python 2): http://docs.python.org/reference/index.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With