[Python-Dev] cpython: Minor clean-ups for heapq.
Michael Urman
murman at gmail.com
Tue May 27 13:45:01 CEST 2014
On Tue, May 27, 2014 at 4:05 AM, Chris Angelico <rosuav at gmail.com> wrote:
> On Tue, May 27, 2014 at 6:58 PM, Serhiy Storchaka <storchaka at gmail.com> wrote:
>> 26.05.14 10:59, raymond.hettinger написав(ла):
>>>
>>> + result = [(elem, i) for i, elem in zip(range(n), it)]
>>
>>
>> Perhaps it is worth to add simple comment explaining why this is not
>> equivalent to just list(zip(it, range(n))). Otherwise it can be
>> unintentionally "optimized" in future.
>>
>
> Where is the difference? I'm very much puzzled now. My first thought
> was based on differing-length iterables in zip, but the docs say it
> stops at the shortest of its args.
Due to how zip stops, it leaves the longer iterable in different places:
>>> it = iter(string.ascii_letters); list(zip(range(3), it)); next(it)
[(0, 'a'), (1, 'b'), (2, 'c')]
'd'
>>> it = iter(string.ascii_letters); list(zip(it, range(3))); next(it)
[('a', 0), ('b', 1), ('c', 2)]
'e'
This seems like a potentially nasty gotcha, but I'm unclear what real
use cases would be impacted.
Michael
More information about the Python-Dev
mailing list