[Python-3000] Builtin iterator type

Mike Orr sluggoster at gmail.com
Mon Nov 20 20:42:34 CET 2006


On 11/20/06, George Sakkis <gsakkis at rutgers.edu> wrote:
> One issue here is the (informal) name of the protocol "file-like",
> which although it adds the suffix "like" and gives a clue it's not
> necessarily about real files, it makes such erroneous assumptions more
> likely.

This is an issue througout Python, one that people have just learned
to live with.  Generally it works because routines use only the
minimum characteristic feature of the original type (file.read() or
file.write(), dict.__getitem__()), and the user has assumed correctly
which features are required (or has looked at the routine's source to
verify what it calls).  But obviously this fails if the routine calls
dict.keys() or dict.pop() and the object doesn't have 100% dict
compatibility.

Older parts of the Python docs describe exactly which methods are
called and with which arguments.  For instance, "a writelines method
that will be called with one argument, and a read method that will be
called with no arguments".  (Because some file-like objects leave out
the optional numeric argument to .read().)  But this is so wordy to
document that most programmers don't go to this level.

I can see the use for MinimalFileLikeForReading,
MinimalFileLikeForWriting, and CompletelyFileLike interfaces.

> Anyone else that would find
> a 'stream' (or 'charstream' or 'StreamMixin' or whatever) class a good
> idea?

Possibly, although we should distinguish between reading and writing.
The traceback module is a good example.  It accepts file-like objects
and calls only file.write.

interface IReadStream:
    def read(self, num_bytes=None)  =>  bytes object

interface IWriteStream:
    def write(self, bytes) => None    # Writes the bytes to the stream.

('bytes object' being the successor to 'str' strings in Python 3000.)

The word "stream" may connote more than this to C/Java programmers --
we'd have to check.

-- 
Mike Orr <sluggoster at gmail.com>


More information about the Python-3000 mailing list