[issue24849] Add __len__ to map, everything in itertools

flying sheep report at bugs.python.org
Wed Aug 12 23:23:26 CEST 2015


flying sheep added the comment:

Hi, and sorry David, but I think you haven’t understood what I was proposing.

Maybe that was too much text and detail to read at once, while skipping the relevant details:

Python has iterators and iterables. iterators are non-reentrant iterables: once they are exhausted, they are useless.

But there are also iterables that create new, iterators whenever iter(iterable) is called (e.g. implicitly in a for loop). They are reentrant. This is why you can loop sequences such as lists more than once.

———————————————————————

One of those reentrant iterables is range(), whose __iter__ functions creates new lazy iterables, which has a __len__, and so on. It even has random access just like a sequence.

Now it’s always entirely possible to *lazily* determine len(chain(range(200), [1,2,5])), which is of course len(range(200)) + len([1,2,5]) = 200 + 3 = 203. No reentrant iterables are necessary here, only iterables with a __len__. (Simply calling len() on them all is sufficient, as it could only create a TypeError which would propagate upwards)

———————————————————————

To reiterate:

1. Lazy doesn’t mean non-reentrant, just like range() demonstrates.
2. I didn’t propose that this works on arbitrary iterables, only that it works if you supply iterables with suitable properties (and throws ValueError otherwise, just like len(some_generator_function()) already does)
3. I know what I’m doing, please trust me and read my proposal carefully ;)

----------
resolution: rejected -> 
status: closed -> open

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24849>
_______________________________________


More information about the Python-bugs-list mailing list