Nested loops is strangely slow, totally at a loss.

Ian Kelly ian.g.kelly at gmail.com
Wed Dec 10 11:01:00 EST 2014


On Wed, Dec 10, 2014 at 1:21 AM, Peter Otten <__peter__ at web.de> wrote:
>
> Ian Kelly wrote:
> > Huh, I wasn't even aware that membership tests worked on iterables with
no
> > __contains__ method. Seems odd to me that 'x in y' should be supported
but
> > not 'len(y)'.
>
> To me
>
> def contains(iterable, value):
>     for item in iterable:
>         if item == value:
>             return True
>     return False
>
> seems to be a perfectly natural default behaviour. I don't see why
>
> >>> 42 in itertools.count()
> True
>
> should require that len(count()) must succeed.

This also seems perfectly natural:

def len(iterable):
    return sum(1 for item in iterable)

My observation is that seems strange to me that one standard sequence
operation should be supported for arbitrary iterators and the other not.

If the objection about `len(count())` is that the call would never return,
I'll point out that `-42 in count()` suffers the same problem.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20141210/1293955f/attachment.html>


More information about the Python-list mailing list