[Python-ideas] make Connections iterable

Oscar Smith oscardssmith at gmail.com
Mon Jan 8 22:05:19 EST 2018


The arguments for including this API is that it allows easy iteration over
the results of a connection allowing it to be used with any of the features
of itertools or any other library accepting iterables. recv is only used in
places where the iterable protocol could be used, so it makes sense for
consistency to use the API shared by the rest of Python.

Oscar Smith

On Mon, Jan 8, 2018 at 7:25 PM, Steven D'Aprano <steve at pearwood.info> wrote:

> 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
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180108/2208d8f1/attachment.html>


More information about the Python-ideas mailing list