当前位置:网站首页>使用lambda在循环中传参时,参数总为同一个值
使用lambda在循环中传参时,参数总为同一个值
2022-07-06 09:14:00 【imxlw00】
当我使用PyQt5时,需要在调用事件时传入参数,于是想到了lambda函数,代码如下:
for i, button in enumerate(self.button_dict.values()):
button.clicked.connect(lambda: self.click_checkbox(i))
此时i都是同一个值。
lambda函数(或称闭包)传递的不是参数的值,而是参数的物理地址,所以lambda传递的参数都会是同一个值。想要解决可以使用functools模块。
解决方案
导入functools模块,使用partial方法。更改的代码如下:
button.clicked.connect(partial(self.click_checkbox, i))
参考:https://blog.csdn.net/qq_34965596/article/details/100579458
边栏推荐
- JDBC原理
- [蓝桥杯2021初赛] 砝码称重
- Request object and response object analysis
- How to build a new project for keil5mdk (with super detailed drawings)
- Record a problem of raspberry pie DNS resolution failure
- 数据库高级学习笔记--SQL语句
- 打开浏览器的同时会在主页外同时打开芒果TV,抖音等网站
- Ansible practical Series III_ Task common commands
- Picture coloring project - deoldify
- Number game
猜你喜欢
一键提取pdf中的表格
Machine learning notes week02 convolutional neural network
MySQL主從複制、讀寫分離
[recommended by bloggers] C WinForm regularly sends email (with source code)
One click extraction of tables in PDF
MySQL master-slave replication, read-write separation
学习问题1:127.0.0.1拒绝了我们的访问
Leetcode 461 Hamming distance
Swagger, Yapi interface management service_ SE
Asp access Shaoxing tourism graduation design website
随机推荐
Tcp/ip protocol (UDP)
JDBC principle
Ansible practical Series III_ Task common commands
AcWing 179.阶乘分解 题解
[蓝桥杯2017初赛]包子凑数
数据库高级学习笔记--SQL语句
Picture coloring project - deoldify
Test objects involved in safety test
记一次某公司面试题:合并有序数组
打开浏览器的同时会在主页外同时打开芒果TV,抖音等网站
Julia 1.6 1.7 common problem solving
Asp access Shaoxing tourism graduation design website
自动机器学习框架介绍与使用(flaml、h2o)
Learn winpwn (2) -- GS protection from scratch
Error connecting to MySQL database: 2059 - authentication plugin 'caching_ sha2_ The solution of 'password'
误删Path变量解决
Django运行报错:Error loading MySQLdb module解决方法
QT creator specifies dependencies
Number game
Error reporting solution - io UnsupportedOperation: can‘t do nonzero end-relative seeks