当前位置:网站首页>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
边栏推荐
- [recommended by bloggers] C WinForm regularly sends email (with source code)
- L2-004 这是二叉搜索树吗? (25 分)
- Armv8-a programming guide MMU (2)
- 保姆级出题教程
- Software testing and quality learning notes 3 -- white box testing
- vs2019 第一个MFC应用程序
- Learn winpwn (3) -- sEH from scratch
- QT creator shape
- [recommended by bloggers] asp Net WebService background data API JSON (with source code)
- AcWing 242. A simple integer problem (tree array + difference)
猜你喜欢
随机推荐
Number game
About string immutability
AcWing 1298.曹冲养猪 题解
Machine learning notes week02 convolutional neural network
[AGC009D]Uninity
Remember the interview algorithm of a company: find the number of times a number appears in an ordered array
记某公司面试算法题:查找一个有序数组某个数字出现的次数
AI benchmark V5 ranking
QT creator test
Ansible practical Series II_ Getting started with Playbook
One click extraction of tables in PDF
MySQL与c语言连接(vs2019版)
數據庫高級學習筆記--SQL語句
人脸识别 face_recognition
MySQL主从复制、读写分离
01项目需求分析 (点餐系统)
[recommended by bloggers] asp Net WebService background data API JSON (with source code)
vs2019 桌面程序快速入门
Learn winpwn (3) -- sEH from scratch
Use dapr to shorten software development cycle and improve production efficiency