classification
Title: lambda not closed on specific value in comprehension
Type: behavior Stage:
Components: Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: David Goffredo, martin.panter
Priority: normal Keywords:

Created on 2016-03-17 05:52 by David Goffredo, last changed 2016-03-17 06:13 by martin.panter. This issue is now closed.

Messages (2)
msg261885 - (view) Author: David Goffredo (David Goffredo) Date: 2016-03-17 05:52
A series of lambdas referring to a variable in a comprehension do not hold distinct values, while conventional higher order function do.

PS C:\Users\David\Desktop> python
Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Caller:
...     def __init__(self, f):
...         self._f = f
...     def start(self):
...         print(self._f())
...
>>> def asFunc(boundValue):
...     def func():
...         return boundValue
...     return func
...
>>> for caller in [Caller(asFunc(x)) for x in range(5)]:
...     caller.start()
...
0
1
2
3
4
>>> for caller in [Caller(lambda: x) for x in range(5)]:
...     caller.start()
...
4
4
4
4
4
>>>
msg261886 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-03-17 06:13
https://docs.python.org/3.5/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result
History
Date User Action Args
2016-03-17 06:13:27martin.pantersetstatus: open -> closed

nosy: + martin.panter
messages: + msg261886

resolution: not a bug
2016-03-17 05:52:46David Goffredocreate