[Python-Dev] cpython: Minor clean-ups for heapq.

Rob Cliffe rob.cliffe at btinternet.com
Wed May 28 01:23:55 CEST 2014


On 27/05/2014 13:44, Tal Einat wrote:
> On Tue, May 27, 2014 at 2:45 PM, Michael Urman <murman at gmail.com> wrote:
>> 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.
> If that's the issue, a comment explaining it should definitely be added.
+1.  FWIW I couldn't see the difference between the 2 codings.  I was 
relieved to see that the zip() doc says
"The left-to-right evaluation order of the iterables is guaranteed."  
Even so, I don't think the reliance on this is obvious.
Rob Cliffe
>
> We could also use islice(enumerate(it), n)) to avoid the confusion.
>
> - Tal
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/rob.cliffe%40btinternet.com
>
>
> -----
> No virus found in this message.
> Checked by AVG - www.avg.com
> Version: 2014.0.4592 / Virus Database: 3950/7568 - Release Date: 05/26/14



More information about the Python-Dev mailing list