The answer given by Andrey is not entirely correct.
In Python, streams are file objects. You can read / write them using the tools defined in the io module. The module also provides interfaces that should be implemented if you want to define a stream object.
Note that the io module distinguishes between three different types of streams that require slightly different interfaces. (They differ mainly in data types.)
StringIO, for example, is an implementation of TextIOBase in memory.
Please note that these interfaces are available in both Python 2 and 3.
source share