Re: [Python-Dev] cpython: Minor clean-ups for heapq.
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. ___ Python-Dev mailing list [email protected] https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] cpython: Minor clean-ups for heapq.
On Tue, May 27, 2014 at 6:58 PM, Serhiy Storchaka 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. ChrisA ___ Python-Dev mailing list [email protected] https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] cpython: Minor clean-ups for heapq.
On Tue, May 27, 2014 at 4:05 AM, Chris Angelico wrote:
> On Tue, May 27, 2014 at 6:58 PM, Serhiy Storchaka 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
___
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] cpython: Minor clean-ups for heapq.
On Tue, May 27, 2014 at 2:45 PM, Michael Urman wrote:
> On Tue, May 27, 2014 at 4:05 AM, Chris Angelico wrote:
>> On Tue, May 27, 2014 at 6:58 PM, Serhiy Storchaka
>> 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.
We could also use islice(enumerate(it), n)) to avoid the confusion.
- Tal
___
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] cpython: Minor clean-ups for heapq.
On 27.05.2014 11:05, Chris Angelico wrote: > On Tue, May 27, 2014 at 6:58 PM, Serhiy Storchaka 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. Nice puzzle. (elem, i) is not (i, elem), though. Thats really hard to see when not looking at it closely. > > ChrisA > ___ > Python-Dev mailing list > [email protected] > https://mail.python.org/mailman/listinfo/python-dev > Unsubscribe: > https://mail.python.org/mailman/options/python-dev/j.wielicki%40sotecware.net > ___ Python-Dev mailing list [email protected] https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] cpython: Minor clean-ups for heapq.
On May 27, 2014, at 1:58 AM, Serhiy Storchaka wrote: > 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. FWIW, that is covered by the test cases. If you substitute list(zip(it, range(n))), the tests fail. Raymond___ Python-Dev mailing list [email protected] https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] cpython: Minor clean-ups for heapq.
On 27/05/2014 13:44, Tal Einat wrote:
On Tue, May 27, 2014 at 2:45 PM, Michael Urman wrote:
On Tue, May 27, 2014 at 4:05 AM, Chris Angelico wrote:
On Tue, May 27, 2014 at 6:58 PM, Serhiy Storchaka 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
[email protected]
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
___
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
