当我运行这个程序时,它会打印NameError global name 'viewAll' is not defined
我是个C程序员,我不知道为什么。在
viewAll(this)
在class binTree
中定义
在平台:Python 2.7在windows 7 64位
#!/usr/bin/python
#-*-coding:gbk-*-
class binTree():
def __init__(this, left = None, right = None, data = 0):
if data == 0:
this = None
else:
this.leftNode = left
this.rightNode = right
this.data = data
def viewAll(this):
if this != None:
print this.data,
viewAll(this.leftNode)
viewAll(this.rightNode)
def creatBT():
temp = input('Please input a number, input "0" for end!')
if temp == 0:
return None
else:
tree = binTree()
tree.data = temp
tree.leftNode = creatBT()
tree.rightNode = creatBT()
return tree
if __name__ == "__main__":
root = creatBT()
root.viewAll()
你需要做一个Python教程你不知道实例对象在Python实例方法中是如何工作的。在
你的问题是:
您需要在要调用它的实例上访问
^{pr2}$viewAll
:我不知道你打算在这里做什么:
但实际上您所做的只是将名称
this
指向该函数调用范围内的None
。它不会更改类实例或函数外部的任何内容。在所以,在
viewAll
中将始终是
True
,因为this
又是您在其上调用的viewAll
的实例,而且不能设置为None
。在请遵守标准并使用self代替this。在
无需测试self(或this)是否为None。在
编辑此解决方案不正确。请看agf的回答。在
相关问题 更多 >
编程相关推荐