为什么尝试调用此模块会导致 "int object is not iterable" 错误?
Why does attempting to call this module result in an "int object is not iterable" error?
我正在尝试学习如何从不同文件中的模块调用函数。
为什么尝试使用返回值会在主程序中出现 'Int object is not iterable' 错误?
import totalages
firstage = int(input('enter age: '))
secondage = int(input('enter age: '))
result = sum(firstage, secondage)
print('together you are', result, 'years old')
####################### 这在一个名为 totalages.py 的单独文件中
def sum(a, b):
return a + b
当 sum 函数包含在 main 中时,代码按预期工作,添加两个输入。但是,如果我将该函数移动到一个单独的文件并尝试导入结果并调用它,我会收到 'int object is not iterable' 错误。为什么?
首先,sum 是一个 python 内置函数,因此您应该将函数重命名为 my_sum
还有两种导入函数的方法
from totalages import my_sum
,它告诉解释器去查看 totalages.py
并导入函数 my_sum
然后你可以直接使用 my_sum(a, b)
import totalages
你需要做 totalages.my_sum(a,b)
现在这里发生了什么,你的 import 语句确实有效,但是你引用了我之前引用的 python sum
内置函数,它接受像列表一样的可迭代对象,但是因为你正在传递它一个整数,你得到你看到的错误 int object is not iterable
如下
In [2]: sum(1+2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-6576b93b138f> in <module>
----> 1 sum(1+2)
TypeError: 'int' object is not iterable
因此请牢记所有这些,您的原始代码将更改为
#Corrected import statement
from totalages import my_sum
firstage = int(input('enter age: '))
secondage = int(input('enter age: '))
result = my_sum(firstage, secondage)
print('together you are', result, 'years old')
而你的 totalages.py 将变为
def my_sum(a, b):
return a + b
或者,如果您使用 import totalages
,另一个选项是
import totalages
firstage = int(input('enter age: '))
secondage = int(input('enter age: '))
result = totalages.my_sum(firstage, secondage)
print('together you are', result, 'years old')
输出将如下所示:
enter age: 20
enter age: 30
together you are 50 years old
您的导入不正确。通过此导入,sum
方法无法由 sum()
调用,而是由 totalages.sum()
调用。您收到此错误消息是因为 python 使用内置方法 sum
,它接收 list
作为参数,而不是您的参数。
以下是您可以使用的一些正确方法:
from totalages import sum
...
sum(a, b)
import totalages
...
totalages.sum(a, b)
顺便说一句,尽量避免使用与内置方法相同的名称。以后会给你带来更多的困惑。
我正在尝试学习如何从不同文件中的模块调用函数。 为什么尝试使用返回值会在主程序中出现 'Int object is not iterable' 错误?
import totalages
firstage = int(input('enter age: '))
secondage = int(input('enter age: '))
result = sum(firstage, secondage)
print('together you are', result, 'years old')
####################### 这在一个名为 totalages.py 的单独文件中
def sum(a, b):
return a + b
当 sum 函数包含在 main 中时,代码按预期工作,添加两个输入。但是,如果我将该函数移动到一个单独的文件并尝试导入结果并调用它,我会收到 'int object is not iterable' 错误。为什么?
首先,sum 是一个 python 内置函数,因此您应该将函数重命名为 my_sum
还有两种导入函数的方法
from totalages import my_sum
,它告诉解释器去查看totalages.py
并导入函数my_sum
然后你可以直接使用my_sum(a, b)
import totalages
你需要做totalages.my_sum(a,b)
现在这里发生了什么,你的 import 语句确实有效,但是你引用了我之前引用的 python sum
内置函数,它接受像列表一样的可迭代对象,但是因为你正在传递它一个整数,你得到你看到的错误 int object is not iterable
如下
In [2]: sum(1+2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-6576b93b138f> in <module>
----> 1 sum(1+2)
TypeError: 'int' object is not iterable
因此请牢记所有这些,您的原始代码将更改为
#Corrected import statement
from totalages import my_sum
firstage = int(input('enter age: '))
secondage = int(input('enter age: '))
result = my_sum(firstage, secondage)
print('together you are', result, 'years old')
而你的 totalages.py 将变为
def my_sum(a, b):
return a + b
或者,如果您使用 import totalages
,另一个选项是
import totalages
firstage = int(input('enter age: '))
secondage = int(input('enter age: '))
result = totalages.my_sum(firstage, secondage)
print('together you are', result, 'years old')
输出将如下所示:
enter age: 20
enter age: 30
together you are 50 years old
您的导入不正确。通过此导入,sum
方法无法由 sum()
调用,而是由 totalages.sum()
调用。您收到此错误消息是因为 python 使用内置方法 sum
,它接收 list
作为参数,而不是您的参数。
以下是您可以使用的一些正确方法:
from totalages import sum
...
sum(a, b)
import totalages
...
totalages.sum(a, b)
顺便说一句,尽量避免使用与内置方法相同的名称。以后会给你带来更多的困惑。