[英]TypeError: 'module' object is not callable for python object
我的代碼中出現以下錯誤。 我正在嘗試制作一個迷宮求解器,我收到的錯誤是:
Traceback (most recent call last):
File "./parseMaze.py", line 29, in <module>
m = maze()
TypeError: 'module' object is not callable
我試圖創建一個名為m
的maze
對象,但顯然我做錯了什么。
我在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)
這是我的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))
我做錯了什么?
它應該是maze.maze()
而不是maze()
。
或者您可以將import
語句更改from maze import maze
。
問題是import語句,你只能導入一個類而不是模塊。 “導入迷宮”是錯誤的而是使用'來自迷宮導入迷宮'
我猜你已經通過設置全局變量“module”覆蓋了內置函數/變量“module”。 只需打印模塊,查看其中的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.