python
type_error
ai_generated
true
类型错误:无法解包非可迭代的 int 对象
TypeError: cannot unpack non-iterable int object
ID: python/typeerror-cannot-unpack-non-iterable
80%修复率
86%置信度
0证据数
2025-02-14首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
根因分析
测试函数返回整数,但调用者尝试将其解包为元组或列表。
English
A test function returns an integer but the caller tries to unpack it as a tuple or list.
解决方案
-
95% 成功率 Return a tuple instead of int
def get_data(): return (1, 2); a, b = get_data()
-
85% 成功率 Unpack only if iterable
result = get_data(); if hasattr(result, '__iter__'): a, b = result
无效尝试
常见但无效的做法:
-
Wrapping return value in a list
60% 失败
If the function returns a single int, wrapping creates a list, but unpacking still expects multiple values.
-
Using *args in function definition
40% 失败
Changes function signature and may break other callers.