python “module' object is not callable”报错
问题描述:
import operator
import BinaryTree
from stackpractise import stack
def buildparsetree(fpexp):
tree=BinaryTree('')
s=stack()
current=tree
for i in fpexp:
if i == '(':
s.push(current)
current.insertleft('')
current=current.getleftchild()
elif i not in '+-*/)':
current.setrootval(int(i))
current=s.pop()
elif i in '+-*/':
current.setrootval(i)
current.insertright('')
s.push(current)
current=current.getrightchild()
elif i == ')':
current=s.pop()
else:
return False
return tree
def evluate(parsetree):
opers={'+':operator.add,'-':operator.sub,'*':operator.mul,'/':operator.truediv()}
leftc=parsetree.getleftchild()
rightc=parsetree.getrightchild()
if leftc and rightc:
fn=opers[parsetree.getrootval()]
return fn(evluate(leftc),evluate(rightc))
else:
return parsetree.getrootval()
t=buildparsetree('(1+(2+3)')
print(evluate(t))
Traceback (most recent call last):
File "D:\pythonwork\Tree\构建解析树.py", line 39, in <module>
t=buildparsetree('(1+(2+3)')
File "D:\pythonwork\Tree\构建解析树.py", line 6, in buildparsetree
tree=BinaryTree('')
TypeError: 'module' object is not callable
答
tree=BinaryTree('')
current=tree
这2个是不是应该改成
current = BinaryTree.tree()
答
BinaryTree引入的地方有问题,如果你想使用BinaryTree文件里的BinaryTree类,请使用from BinaryTree import BinaryTree
答
可能是你的文件名和函数名重合了,我遇到这样的报错都是文件名和函数名重合才报错的
https://blog.csdn.net/u010899985/article/details/80302733?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control
有用的话点一下采纳