当前位置:网站首页>使用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
边栏推荐
- Generate PDM file from Navicat export table
- Use dapr to shorten software development cycle and improve production efficiency
- Machine learning notes week02 convolutional neural network
- 安装numpy问题总结
- Tcp/ip protocol (UDP)
- 一键提取pdf中的表格
- One click extraction of tables in PDF
- 记一次某公司面试题:合并有序数组
- Leetcode 461 Hamming distance
- 安全测试涉及的测试对象
猜你喜欢

neo4j安装教程

LeetCode #461 汉明距离

Idea import / export settings file

csdn-Markdown编辑器

【博主推荐】C#生成好看的二维码(附源码)

MySQL主从复制、读写分离

Django运行报错:Error loading MySQLdb module解决方法

Error connecting to MySQL database: 2059 - authentication plugin 'caching_ sha2_ The solution of 'password'

机器学习笔记-Week02-卷积神经网络
![[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP](/img/7d/8cbbd2f328a10808319458a96fa5ec.jpg)
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
随机推荐
A trip to Macao - > see the world from a non line city to Macao
一键提取pdf中的表格
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
QT creator runs the Valgrind tool on external applications
Cookie setting three-day secret free login (run tutorial)
MySQL主從複制、讀寫分離
Antlr4 uses keywords as identifiers
图片上色项目 —— Deoldify
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
[free setup] asp Net online course selection system design and Implementation (source code +lunwen)
[蓝桥杯2021初赛] 砝码称重
Software testing - interview question sharing
NPM an error NPM err code enoent NPM err syscall open
AcWing 1294.樱花 题解
软件测试-面试题分享
Asp access Shaoxing tourism graduation design website
Django running error: error loading mysqldb module solution
Ansible实战系列二 _ Playbook入门
Ubuntu 20.04 安装 MySQL
Solution: log4j:warn please initialize the log4j system properly