[issue47028] Incorrect behaviour when zipping a bunch of maps

2022-03-15 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

https://bugs.python.org/issue45469 is similar. Thanks for the report, but I'll 
go ahead and close this.

--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47028] Incorrect behaviour when zipping a bunch of maps

2022-03-15 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

There's an FAQ entry here: 
https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47028] Incorrect behaviour when zipping a bunch of maps

2022-03-15 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

This is because i is not captured by the function definition. `lambda x: x**i` 
always makes the "input to the ith power" function, never the "input to the 3rd 
power" function, even if i happens to be 3 right now.

Consider replacing `lambda x: x**i` with `lambda x, i=i: x**i` to explicitly 
capture the current value of i as a default.

Changing the scoping rules now would be a big backwards-incompatible change.

--
nosy: +Dennis Sweeney

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47028] Incorrect behaviour when zipping a bunch of maps

2022-03-15 Thread John K.


New submission from John K. :

map(f, itr) is supposed to be the lazy way to do [f(val) for val in itr]. 
However, when unpacking and zipping a list of maps, I get a different result 
from when evaluating eagerly. More precisely:

Python 3.10.2 | packaged by conda-forge | (main, Mar  8 2022, 15:53:57) [GCC 
9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> maps = [map(lambda x: x**i, range(7)) for i in range(5)]
>>> for z in zip(*maps):
... print(z)
...
(0, 0, 0, 0, 0)
(1, 1, 1, 1, 1)
(16, 16, 16, 16, 16)
(81, 81, 81, 81, 81)
(256, 256, 256, 256, 256)
(625, 625, 625, 625, 625)
(1296, 1296, 1296, 1296, 1296)
>>> lists = [[x**i for x in range(7)] for i in range(5)]
>>> for z in zip(*lists):
... print(z)
...
(1, 0, 0, 0, 0)
(1, 1, 1, 1, 1)
(1, 2, 4, 8, 16)
(1, 3, 9, 27, 81)
(1, 4, 16, 64, 256)
(1, 5, 25, 125, 625)
(1, 6, 36, 216, 1296)

--
components: Interpreter Core
messages: 415256
nosy: khoodolphin
priority: normal
severity: normal
status: open
title: Incorrect behaviour when zipping a bunch of maps
type: behavior
versions: Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com