"类型错误:"列表"对象不可调用",即使我没有重命名任何内置函数



我有一个非常简单的代码:

g = ('f','a')
h = list(g)

我得到这个回溯:

Traceback (most recent call last):
File "<ipython-input-87-7941c56fab04>", line 1, in <module>
runfile('/Users/username/Desktop/untitled3.py', wdir='/Users/username/Desktop')
File "/Users/username/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "/Users/username/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/Users/username/Desktop/untitled3.py", line 10, in <module>
h = list(g)
TypeError: 'list' object is not callable

请原谅我的无知;我在这里一定遗漏了一些非常基本的东西。有人能帮我了解发生了什么事吗?我没有足够的东西来尝试这个简单的代码。

我正在运行Python 3.6.0。

帮助您更好地将其可视化。你可能在你的iPython:中做了这样的事情

>>> list = [1, 2, 3]
>>> g = ('f', 'a')
>>> h = list(g)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

在执行此操作时,您将文字列表设置为列表,因此,当尝试根据()调用时,会出现此错误,因为文字[]没有__call__方法:

>>> your_list = [1, 2, 3]
>>> your_list()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

调用内置的列表类将为您提供一个list对象。因此,如果你重置你的解释器,并尝试这样做,你会进一步看到正在发生的事情:

>>> a_list = list()
>>> a_list()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

我相信您一定在源文件中使用了list作为变量。这是我复制错误的转储

>>> a = ('g','h')
>>> b = list(a)
>>> b
['g', 'h']
>>> list = [1,2]
>>> c = list(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

请注意,当我在定义一个名为list的变量之前定义了"b"时,它返回了一个列表,但当我在将变量命名为list后尝试将"c"定义为list(a)时出现了错误。这是因为python将list关键字解释为一个变量。

最新更新