[Python-ideas] make Connections iterable

Steven D'Aprano steve at pearwood.info
Mon Jan 8 20:25:27 EST 2018


On Mon, Jan 08, 2018 at 10:17:30AM -0600, Oscar Smith wrote:
> I am currently working on a program where it would be really useful if a
> connection had a __next__ method, because then it would be much easier to
> iterate over.

What sort of connection are you referring to?


> It would just be an alias to recv, but would allow you to do
> things like merging the results of connections using heapq.merge that
> currently are highly non-trivial to accomplish.

This gives you an iterator which repeatedly calls connection.recv until 
it raises a FooException, then ends.

def conn_iter(connection):
    try:
        while True:
            yield connection.recv()
    except FooException:  # FIXME -- what does recv actually raise?
        return

Doesn't seem "highly non-trivial" to me. Have I missed something?


> Is there a reason this API isn't supported?

You are asking the wrong question. Adding APIs isn't "default allow", 
where there has to be a reason to *not* support it otherwise it gets 
added. It is "default deny" -- there has to be a good reason to add it, 
otherwise it gets left out. YAGNI is an excellent design principle, as 
it is easier to add a useful API later, than to remove an unnecessary or 
poorly designed one.

So the question needs to be:

"Is this a good enough reason to support this API?"

Maybe, maybe not. Not every trivial wrapper function needs to be a 
method.

But perhaps this is an exception: perhaps iterability is such a common 
and useful API for connections that it should be added, for the same 
reason that files are iterable.

Care to elaborate on why this would be useful and why the generator I 
showed above isn't satisfactory?

-- 
Steve


More information about the Python-ideas mailing list