Using Barcode maker for Font Control to generate, create Barcode image in Font applications.

www.OnBarcode.com

Here s an example of its use: r = repeater(42) r.next() 42 r.send("Hello, world!") "Hello, world!" Note the use of parentheses around the yield expression. While not strictly necessary in some cases, it is probably better to be safe than sorry, and simply always enclose yield expressions in parentheses if you are using the return value in some way. Generators also have two other methods (in Python 2.5 and later): The throw method (called with an exception type, an optional value and traceback object) is used to raise an exception inside the generator (at the yield expression). The close method (called with no arguments) is used to stop the generator. The close method (which is also called by the Python garbage collector, when needed) is also based on exceptions. It raises the GeneratorExit exception at the yield point, so if you want to have some cleanup code in your generator, you can wrap your yield in a try/finally statement. If you wish, you can also catch the GeneratorExit exception, but then you must reraise it (possibly after cleaning up a bit), raise another exception, or simply return. Trying to yield a value from a generator after close has been called on it will result in a RuntimeError.

Using Barcode encoder for Java Control to generate, create PDF 417 image in Java applications.

www.OnBarcode.com

If you need to use an older version of Python, generators aren t available. What follows is a simple recipe for simulating them with normal functions. Starting with the code for the generator, begin by inserting the following line at the beginning of the function body: result = [] If the code already uses the name result, you should come up with another. (Using a more descriptive name may be a good idea anyway.) Then replace all lines of this form: yield some_expression with this: result.append(some_expression)

Using Barcode creation for BIRT reports Control to generate, create Barcode image in BIRT applications.

www.OnBarcode.com

Finally, at the end of the function, add this line: return result Although this may not work with all generators, it works with most. (For example, it fails with infinite generators, which of course can t stuff their values into a list.) Here is the flatten generator rewritten as a plain function: def flatten(nested): result = [] try: # Don't iterate over string-like objects: try: nested + '' except TypeError: pass else: raise TypeError for sublist in nested: for element in flatten(sublist): result.append(element) except TypeError: result.append(nested) return result

Using Barcode encoder for Android Control to generate, create Code 3 of 9 image in Android applications.

www.OnBarcode.com

Generators and Backtracking

Generators are ideal for complex recursive algorithms that gradually build a result. Without generators, these algorithms usually require you to pass a half-built solution around as an extra parameter so that the recursive calls can build on it. With generators, all the recursive calls need to do is yield their part. That is what I did with the preceding recursive version of flatten, and you can use the exact same strategy to traverse graphs and tree structures. In some applications, however, you don t get the answer right away; you need to try several alternatives, and you need to do that on every level in your recursion. To draw a parallel from real life, imagine that you have an important meeting to attend. You re not sure where it is, but you have two doors in front of you, and the meeting room has to be behind one of them. You choose the left and step through. There, you face another two doors. You choose the left, but it turns out to be wrong. So you backtrack, and choose the right door, which also turns out to be wrong (excuse the pun). So, you backtrack again, to the point where you started, ready to try the right door there.