【问题标题】:python- why does my program tells me it's not callablepython-为什么我的程序告诉我它不可调用
【发布时间】:2019-03-09 13:01:42
【问题描述】:

我在 python 中创建了一个类,当我尝试将它调用到另一个 python 文件中时(在导入它之后)它不会将它识别为一个类而是一个对象,然后它告诉我我的类是不可调用的

这是我的课:

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

这里是我尝试调用它的地方:

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)

最后是错误:

x=Cell()
TypeError: 'module' object is not callable

谁能帮我解决这个问题,即使我的老师也不理解我的错误

【问题讨论】:

  • 你可能需要使用from cell import Cell
  • 我认为你应该像from &lt;your_module_name&gt; import Cell这样导入你的类
  • Cell.Cell()from Cell import Cell
  • 该问题不可重现。
  • 您导入了一个模块。模块不可调用。

标签: python xcode python-2.7 debugging helper


【解决方案1】:

Cell 已用于您的导入模块和您的类。根据错误,python已将其映射到模块名称。因此,当您编写Cell() 时,它会尝试将模块名称用作函数,而不是调用类构造函数。

如果Cell 类在Cell 模块内,请改用Cell.Cell(),或将导入更改为from Cell import Cell。否则,重命名模块或类。

【讨论】:

    【解决方案2】:

    您的导入语句是错误的,您正在导入一个名为 Cell 的模块,而不是您的 Cell 类。您应该使用小写的文件名并像这样导入它:

    from cell import Cell
    
    
    test = Cell()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-11
      • 2022-06-16
      • 2019-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多