def countdown( )while yield " -minus % \nn - yield "kaboom!\nproducing an output stream in this manner provides great flexibility because the production of the output stream is decoupled from the code that actually directs the stream to its intended destination for exampleif you wanted to route the above output to file fyou could do thiscount countdown( writelines(countifinsteadyou wanted to redirect the output across socket syou could do thisfor chunk in counts sendall(chunkorif you simply wanted to capture all of the output in stringyou could do thisout "join(countmore advanced applications can use this approach to implement their own / buffering for examplea generator could be emitting small text fragmentsbut another function could be collecting the fragments into large buffers to create largermore efficient / operationchunks [buffered_size for chunk in countchunks append(chunkbuffered_size +len(chunkif buffered_size >maxbuffersizeoutf write("join(chunks)chunks clear(buffered_size outf write("join(chunksfor programs that are routing output to files or network connectionsa generator approach can also result in significant reduction in memory use because the entire output stream can often be generated and processed in small fragments as opposed to being first collected into one large output string or list of strings this approach to output is sometimes seen when writing programs that interact with the python web services gateway interface (wsgithat' used to communicate between components in certain web frameworks unicode string handling common problem associated with / handling is that of dealing with international characters represented as unicode if you have string of raw bytes containing an encoded representation of unicode stringuse the decode([encoding [,errors]]method to convert it into proper unicode string to convert unicode stringuto an encoded byte stringuse the string method encode([encoding [errors]]both of these conversion operators require the use of special encoding name that specifies how unicode character values are mapped to sequence of -bit characters in byte stringsand vice versa the encoding parameter is specified as string lib fl ff