python - python 3 typeError:'NoneType'对象不可迭代
问题描述
我编写了下面的代码来查找 *args 的继承。但我有一个错误。我真的不明白这个错误,因为我有输入,但他们不是没有。
def third(*args, option=True):
if len(args) == 2:
word1, word2 = args
else:
word1 = args[0]
if option:
return word1, word2
else:
return word1
def hello(data, *args, option=True):
print("the data is:", data)
A, B = third(*args, option=True)
print("the args are:", A, B)
def world(small, *args, option=True):
return hello(small, *args)
if __name__ == "main":
world("data","prediction")
输出:
the data is: data
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in world
File "<stdin>", line 3, in hello
TypeError: 'NoneType' object is not iterable
解决方案
基本上你传递 options=True 这意味着“第三个”函数应该总是返回 word1 和 word2。但是 len of args 是一个,所以 word2 根据你的 if len(args) == 2 条件不存在。
所以“第三个”函数只返回 word1。在您的“hello”函数中,您试图通过 A, B =third(arguments) 方法将该单个元素映射到两个变量“A”和“B”,该方法迭代函数的返回值,但它无法这样做,因为第三是返回一个元素或一个错误值(因为您试图返回不存在的 word2)。这就是发生此错误的原因
推荐阅读
- excel - 如何从 2 列中获取最大数量和名称?(Excel)
- python - 在每行上打印有限数量的列表(python)
- postgresql - 我真的需要 Bridge Tables 上的标识字段吗?
- javascript - 绳索物理 - 绳索在重力作用下无法正确移动
- wso2 - WSO2 身份服务器 - 用户禁用在服务器重新启动之前不起作用
- html - 如何设计表头单元格在顶行和第一列?
- c - 如何从分配函数的外部访问结构指针的值?
- python - 如何计算边界框坐标?
- javascript - 如何在反应碳设计图表中设置颜色
- python - 使用命令更改 discord.py 机器人