简体   繁体   中英

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

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. According to the error, python has mapped it to the module name. So, when you are writing Cell() , it tries to use the module name as a function, instead of calling the class constructor.

If the class Cell is inside the Cell module, use Cell.Cell() instead, or change your import to 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. You should use lower case for your filenames and import it like so:

from cell import Cell


test = Cell()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM