Read this first . It's about lambda x=x: foo(x) catching x even in a for loop.
This is a window with a label and two buttons generated in a for loop. When the button is pressed, this name appears in the label.
If we use the usual lambda: label.setText("button -- " + str(i)) , then the result will be the last i in the loop, regardless of which button is pressed:

And it is right.
When we go to lambda i=i: label.setText("button -- " + str(i)) (snipet) and expect that everything will be fine now, the result:
![lambda i = i: foo (i)]](https://fooobar.com//img/8e76cee95c897355e34e81745d1c8ebd.jpg)
Lying!
Where does this False come from?
import sys from PyQt4.QtGui import * class MainWindow(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) vbox = QVBoxLayout(self)
Why doesn't this solution work as it should? What does this False mean?
I know that you can do foo_factory as in the first link, but the question is what is wrong with lambda i=i: foo(i)
source share