[英]python- why does my program tells me it's not callable
I made a class in python and when I tried to call it into another python file (after importing it) it doesn't recognizes it as a class but as an object and then it tells me that my class is not callable我在 python 中创建了一个类,当我尝试将它调用到另一个 python 文件中时(在导入它之后)它不会将它识别为一个类而是一个对象,然后它告诉我我的类不可调用
here is my class:这是我的课:
class Cell:
def __init__(self,value=9,isVissible=False):
self.value=value
self.isVisible=isVissible
def setValue(self,value):
self.value=value
def setVisible(self):
self.visible=True
and here is where I tried to call it:这是我试图称呼它的地方:
import Cell,random
class Board:
def __init__(self):
self.board = []
for i in range(12):
a = []
for j in range(12):
x = Cell() <=== right here it's an error
.
.
.(the rest of my program)
and finally here is the error:最后这里是错误:
x=Cell()
TypeError: 'module' object is not callable
can anyone help me fix this, even my teacher didn't understand my mistake谁能帮我解决这个问题,即使我的老师也不明白我的错误
The Cell
has been used for both your imported module and your class. Cell
已用于您导入的模块和您的类。 According to the error, python has mapped it to the module name.根据错误,python已经将其映射到模块名称。 So, when you are writing Cell()
, it tries to use the module name as a function, instead of calling the class constructor.因此,当您编写Cell()
,它会尝试将模块名称用作函数,而不是调用类构造函数。
If the class Cell
is inside the Cell
module, use Cell.Cell()
instead, or change your import to from Cell import Cell
.如果类Cell
在Cell
模块内,请改用Cell.Cell()
,或者将导入更改为from Cell import Cell
。 Otherwise, rename either the module or the class.否则,重命名模块或类。
Your import statement is wrong, you're importing a module called Cell instead of your Cell class.您的导入语句是错误的,您正在导入一个名为 Cell 的模块而不是您的 Cell 类。 You should use lower case for your filenames and import it like so:您应该使用小写作为文件名并像这样导入:
from cell import Cell
test = Cell()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.