当前位置:网站首页>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
边栏推荐
- One click extraction of tables in PDF
- Introduction and use of automatic machine learning framework (flaml, H2O)
- AI benchmark V5 ranking
- Test objects involved in safety test
- C语言读取BMP文件
- MySQL与c语言连接(vs2019版)
- QT creator uses Valgrind code analysis tool
- QT creator test
- L2-006 树的遍历 (25 分)
- Remember the interview algorithm of a company: find the number of times a number appears in an ordered array
猜你喜欢

LeetCode #461 汉明距离

PHP - whether the setting error displays -php xxx When PHP executes, there is no code exception prompt

02-项目实战之后台员工信息管理

Solution: log4j:warn please initialize the log4j system properly

Request object and response object analysis

About string immutability
C语言读取BMP文件

保姆级出题教程
![[recommended by bloggers] C WinForm regularly sends email (with source code)](/img/5d/57f8599a4f02c569c6c3f4bcb8b739.png)
[recommended by bloggers] C WinForm regularly sends email (with source code)

Neo4j installation tutorial
随机推荐
数据库高级学习笔记--SQL语句
DICOM: Overview
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
Swagger, Yapi interface management service_ SE
[蓝桥杯2017初赛]方格分割
AcWing 1294.樱花 题解
ImportError: libmysqlclient. so. 20: Cannot open shared object file: no such file or directory solution
保姆级出题教程
Why can't STM32 download the program
Armv8-a programming guide MMU (2)
Error connecting to MySQL database: 2059 - authentication plugin 'caching_ sha2_ The solution of 'password'
软件测试-面试题分享
Number game
JDBC原理
MySQL other hosts cannot connect to the local database
Did you forget to register or load this tag 报错解决方法
[download app for free]ineukernel OCR image data recognition and acquisition principle and product application
引入了junit为什么还是用不了@Test注解
Why can't I use the @test annotation after introducing JUnit
MySQL master-slave replication, read-write separation