类型错误:“模块”对象不可用于 python 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18928826/
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
TypeError: 'module' object is not callable for python object
提问by user2604504
I'm getting the following error in my code. I am attempting to make a maze solver and I am getting an error that says:
我的代码中出现以下错误。我正在尝试制作一个迷宫求解器,但收到一个错误消息:
Traceback (most recent call last):
File "./parseMaze.py", line 29, in <module>
m = maze()
TypeError: 'module' object is not callable
I am attempting to create a mazeobject called mbut apparently I'm doing something wrong.
我正在尝试创建一个maze名为的对象,m但显然我做错了什么。
I wrote these lines in parseMaze.py
我写了这些行 parseMaze.py
#!/user/bin/env python
import sys
import cell
import maze
import array
# open file and parse characters
with open(sys.argv[-1]) as f:
# local variables
x = 0 # x length
y = 0 # y length
char = [] # array to hold the character through maze
iCell = []# not sure if I need
# go through file
while True:
c = f.read(1)
if not c:
break
char.append(c)
if c == '\n':
y += 1
if c != '\n':
x += 1
print y
x = x/y
print x
m = maze()
m.setDim(x,y)
for i in range (len(char)):
if char(i) == ' ':
m.addCell(i, 0)
elif char(i) == '%':
m.addCell(i, 1)
elif char(i) == 'P':
m.addCell(i, 2)
elif char(i) == '.':
m.addCell(i, 3)
else:
print "do newline"
print str(m.cells)
Here is my maze.pyfile which contains the maze class:
这是我的maze.py文件,其中包含迷宫类:
#! /user/bin/env python
class maze:
w = 0
h = 0
size = 0
cells =[]
# width and height variables of the maze
def _init_(self):
w = 0
h = 0
size = 0
cells =[]
# set dimensions of maze
def _init_(self, width, height):
self.w = width
self.w = height
self.size = width*height
# find index based off row major order
def findRowMajor(self, x, y):
return (y*w)+x
# add a cell to the maze
def addCell(self, index, state):
cells.append(cell(index, state))
What is it that I am doing wrong?
我做错了什么?
回答by Bleeding Fingers
It should be maze.maze()instead of maze().
它应该maze.maze()代替maze().
Or you could change your importstatement to from maze import maze.
或者您可以将您的import声明更改为from maze import maze.
回答by Felix
The problem is the import statement, you can only import a class not module. 'import maze' is wrong rather use 'from maze import maze'
问题是导入语句,你只能导入一个类而不是模块。'import maze' 是错误的,而是使用 'from maze import maze'
回答by umairhhhs
I guess you have overridden the builtin function/variable "module" by setting the global variable "module". just print the module see whats in it.
我猜您已经通过设置全局变量“module”覆盖了内置函数/变量“module”。只需打印模块,看看里面有什么。

