Python “类型错误:‘设置’对象不可调用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31202534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:38:04  来源:igfitidea点击:

"TypeError: 'set' object is not callable"

pythonmacosamazon-web-servicesipythontypeerror

提问by user2762934

I have two iPython notebook installations. One on an AWS Micro Instance, the second using Anaconda on my Macbook (OS X Yosemite). I encountered a difference in the way both of them handle the following code:

我有两个 iPython 笔记本安装。一个在 AWS 微型实例上,第二个在我的 Macbook (OS X Yosemite) 上使用 Anaconda。我在他们处理以下代码的方式上遇到了差异:

my_list = [1, 2, 3, 0, 5, 10, 11, 1, 5]
your_list = [1, 2, 3, 0, 12, 13]
my_set = set(my_list)
your_set = set(your_list)
print my_set
print len(my_set)
print len(my_list)

On iPython-AWS, my output is:

在 iPython-AWS 上,我的输出是:

set([0, 1, 2, 3, 5, 10, 11])
7
9

On iPython-Macbook, my output is:

在 iPython-Macbook 上,我的输出是:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-cd060f1b0bde> in <module>()
      1 my_list = [1, 2, 3, 0, 5, 10, 11, 1, 5]
      2 your_list = [1, 2, 3, 0, 12, 13]
----> 3 my_set = set(my_list)
      4 your_set = set(your_list)
      5 print my_set

TypeError: 'set' object is not callable

Additionally, these are the installation details, if relevant: 1. iPython on AWS Micro Instance: http://i.stack.imgur.com/qYrq8.png

此外,这些是安装详细信息(如果相关): 1. AWS 微型实例上的 iPython:http://i.stack.imgur.com/qYrq8.png

  1. iPython Notebook on Macbook - http://i.stack.imgur.com/Q6Id5.png
  1. Macbook 上的 iPython 笔记本 - http://i.stack.imgur.com/Q6Id5.png

I cannot seem to find the reason for this difference, although I did come across many threads on Stackoverflow regarding the "TypeError: 'set' object is not callable" issue. I will appreciate any help in understanding why this is so, and if there is anything I can do to ensure my code runs on both installations.

我似乎无法找到这种差异的原因,尽管我确实在 Stackoverflow 上遇到了许多关于“TypeError: 'set' object is not callable”问题的线程。我将不胜感激任何帮助理解为什么会这样,如果我能做些什么来确保我的代码在两个安装上运行。

采纳答案by Anand S Kumar

This error indicates that you may have defined a set with the variable name as set, if you did so, that would overwrite the built-infunction set.

此错误表明您可能定义了一个变量名为 的集合set,如果这样做,将覆盖built-in函数set

Example of this issue occuring -

发生此问题的示例 -

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

回答by Xavier Sebastian Vaca Ordo?ez

You can restart the kernel in the top menu. Kernel->Restart

您可以在顶部菜单中重新启动内核。内核->重启

回答by user13292071

Simply remove any predefined variable you have assigned, may be intentionally or unintentionally & the error will be vanished. I have wrongly placed = signbetween print statement & brackets like print=(A|B);due to which it was happening. After clearing print variable it was fine.

只需删除您分配的任何预定义变量,可能是有意或无意的,错误就会消失。我错误地placed = sign在打印语句和括号之间print=(A|B);,因为它正在发生。清除打印变量后就好了。