As of Python2.5, {{{StreamWriter}}} wraps (contains) a stream.  It defines {{{write}}} and other respective methods to "encode" the data and pass the result to the stream.  It exposes all other methods of the stream instance.

Pseudocode of the {{{codecs.StreamWriter}}} definition:

{{{
#!python
class StreamWriter(Codec):
    def __init__(self, stream):
        ....

    def write(self, data):
        return stream.write(self.encode(data))
}}}


The {{{encode}}} method normally converts values of type {{{unicode}}}  to {{{str}}}s.

Codec modules will attach the {{{encode}}} method to the class definition derived from {{{StreamWriter}}} during the initialization.  An excerpt from {{{encodings.utf_8.StreamWriter}}}:

{{{
#!python
class StreamWriter(codecs.StreamWriter):
    encode = codecs.utf_8_encode
}}}

----
See also: StreamReader, StreamReaderWriter, StreamRecoder.
----
CategoryUnicode