New GitHub issue #105175 from gudn:<br>
<hr>
<pre>
<!--
If you're new to Python and you're not sure whether what you're experiencing is a bug, the CPython issue tracker is not
the right place to seek help. Consider the following options instead:
- reading the Python tutorial: https://docs.python.org/3/tutorial/
- posting in the "Users" category on discuss.python.org: https://discuss.python.org/c/users/7
- emailing the Python-list mailing list: https://mail.python.org/mailman/listinfo/python-list
- searching our issue tracker (https://github.com/python/cpython/issues) to see if
your problem has already been reported
-->
# Bug report
I'm writing combinatoric parsing library and noticed one bug when using lambda in list comprehensions. I don't know what is real bug cause, but it also exists in lambda that capture loop variable.
Following code demonstrates the behavior:
```python
def constantly(x):
return lambda _: x
def cmap(f, c):
return lambda x: f(c(x))
data = {'a': 1, 'b': 2, 'c': 3}
print('Build list with list comprehensions')
ret = [cmap(lambda y: (k, y), constantly(x)) for k, x in data.items()]
for r in ret:
print(r(-100))
print('Build list from plain for-loop')
ret2 = []
for k, x in data.items():
c = cmap(lambda y: (k, y), constantly(x))
ret2.append(c)
for r in ret2:
print(r(-100))
print('Use function without storing')
for k, x in data.items():
c = cmap(lambda y: (k, y), constantly(x))
print(c(-100))
```
Results:
```
Build list with list comprehensions
('c', 1)
('c', 2)
('c', 3)
Build list from plain for-loop
('c', 1)
('c', 2)
('c', 3)
Use function without storing
('a', 1)
('b', 2)
('c', 3)
```
# Your environment
<!-- Include as many relevant details as possible about the environment you experienced the bug in -->
- CPython versions tested on: 3.11.3 (from docker.io/python:3.11.3)
- Operating system and architecture: Manjaro, Linux 6.1, x86_64
<!--
You can freely edit this text. Remove any lines you believe are unnecessary.
-->
</pre>
<hr>
<a href="https://github.com/python/cpython/issues/105175">View on GitHub</a>
<p>Labels: type-bug</p>
<p>Assignee: </p>