python type_error ai_generated true

类型错误:无法解包非可迭代的 int 对象

TypeError: cannot unpack non-iterable int object

ID: python/typeerror-cannot-unpack-non-iterable

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  1. 95% 成功率 Return a tuple instead of int
    def get_data(): return (1, 2); a, b = get_data()
  2. 85% 成功率 Unpack only if iterable
    result = get_data(); if hasattr(result, '__iter__'): a, b = result

无效尝试

常见但无效的做法:

  1. Wrapping return value in a list 60% 失败

    If the function returns a single int, wrapping creates a list, but unpacking still expects multiple values.

  2. Using *args in function definition 40% 失败

    Changes function signature and may break other callers.