当前位置:网站首页>When using lambda to pass parameters in a loop, the parameters are always the same value
When using lambda to pass parameters in a loop, the parameters are always the same value
2022-07-06 11:24:00 【imxlw00】
When I use PyQt5 when , You need to pass in parameters when calling events , So I thought of lambda function , The code is as follows :
for i, button in enumerate(self.button_dict.values()):
button.clicked.connect(lambda: self.click_checkbox(i))
here i It's all the same value .
lambda function ( Or closure ) What is passed is not the value of the parameter , It is the physical address of the parameter , therefore lambda The parameters passed will all be the same value . If you want to solve it, you can use functools modular .
Solution
Import functools modular , Use partial Method . The changed code is as follows :
button.clicked.connect(partial(self.click_checkbox, i))
Reference resources :https://blog.csdn.net/qq_34965596/article/details/100579458
边栏推荐
- Unable to call numpy in pycharm, with an error modulenotfounderror: no module named 'numpy‘
- MySQL与c语言连接(vs2019版)
- When you open the browser, you will also open mango TV, Tiktok and other websites outside the home page
- 图像识别问题 — pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your path
- 数数字游戏
- 人脸识别 face_recognition
- 牛客Novice月赛40
- Neo4j installation tutorial
- Attention apply personal understanding to images
- Case analysis of data inconsistency caused by Pt OSC table change
猜你喜欢
随机推荐
Picture coloring project - deoldify
Are you monitored by the company for sending resumes and logging in to job search websites? Deeply convinced that the product of "behavior awareness system ba" has not been retrieved on the official w
Test objects involved in safety test
AcWing 242. A simple integer problem (tree array + difference)
About string immutability
Ansible实战系列三 _ task常用命令
Install mongdb tutorial and redis tutorial under Windows
Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.
Some notes of MySQL
MySQL主从复制、读写分离
安装numpy问题总结
QT creator create button
Codeforces Round #771 (Div. 2)
[recommended by bloggers] C WinForm regularly sends email (with source code)
In the era of DFI dividends, can TGP become a new benchmark for future DFI?
软件测试与质量学习笔记3--白盒测试
Classes in C #
Swagger, Yapi interface management service_ SE
记某公司面试算法题:查找一个有序数组某个数字出现的次数
自动机器学习框架介绍与使用(flaml、h2o)