
C Anthony Risinger wrote:
* Perhaps existence of `__len__` should influence unpacking? There is a semantic difference (and typically a visual one too) between 1-to-1 matching a fixed-width sequence/container on the RHS to identifiers on the LHS, even if they look similar (ie. "if RHS has a length make it fit, otherwise don't").
-1. There's a convention that an iterator can implement __len__ to provide a hint about the number of items it will return (useful for preallocating space, etc.) It would be very annoying if such an iterator behaved differently from other iterators when unpacking. Another thing is that the proposed feature will be useful on non-iterator iterables as well, since it saves the overhead of unpacking the rest of the items only to throw them away.
While I like the original proposal, adding basic slice support to iterables is also a nice idea.
It's not as nice as it seems. You're asking for __getitem__ to be made part of the iterator protocol, which would be a huge change affecting all existing iterators. Otherwise, it would just be a piecemeal affair. Some iterators would support slicing and some wouldn't, so you couldn't rely on it. -- Greg