为什么我得到“实例没有属性'__getitem__'”错误?

时间:2010-03-29 22:38:12

标签: python

以下是代码:

class BinaryTree:
    def __init__(self,rootObj):
        self.key = rootObj
        self.left = None
        self.right = None
        root = [self.key, self.left, self.right]

    def getRootVal(root):
        return root[0]

    def setRootVal(newVal):
        root[0] = newVal

    def getLeftChild(root):
        return root[1]

    def getRightChild(root):
        return root[2]

    def insertLeft(self,newNode):
        if self.left == None:
                self.left = BinaryTree(newNode)
        else:
            t = BinaryTree(newNode)
            t.left = self.left
            self.left = t

    def insertRight(self,newNode):
        if self.right == None:
            self.right = BinaryTree(newNode)
        else:
            t = BinaryTree(newNode)
            t.right = self.right
            self.right = t

def buildParseTree(fpexp):
    fplist = fpexp.split()
    pStack = Stack()
    eTree = BinaryTree('')
    pStack.push(eTree)
    currentTree = eTree
    for i in fplist:
        if i == '(':
            currentTree.insertLeft('')
            pStack.push(currentTree)
            currentTree = currentTree.getLeftChild()
        elif i not in '+-*/)':
            currentTree.setRootVal(eval(i))
            parent = pStack.pop()
            currentTree = parent
        elif i in '+-*/':
            currentTree.setRootVal(i)
            currentTree.insertRight('')
            pStack.push(currentTree)
            currentTree = currentTree.getRightChild()
        elif i == ')':
            currentTree = pStack.pop()
        else:
            print "error:  I don't recognize " + i
    return eTree

def postorder(tree):
    if tree != None:
        postorder(tree.getLeftChild())
        postorder(tree.getRightChild())
        print tree.getRootVal()

def preorder(self):
    print self.key
    if self.left:
        self.left.preorder()
    if self.right:
        self.right.preorder()

def inorder(tree):
    if tree != None:
        inorder(tree.getLeftChild())
        print tree.getRootVal()
        inorder(tree.getRightChild())

class Stack:
    def __init__(self):
        self.items = []

    def isEmpty(self):
        return self.items == []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def peek(self):
        return self.items[len(self.items)-1]

    def size(self):
        return len(self.items)

def main():
    parseData = raw_input( "Please enter the problem you wished parsed.(NOTE: problem must have parenthesis to seperate each binary grouping and must be spaced out.) " )
    tree = buildParseTree(parseData)
    print( "The post order is: ", + postorder(tree))
    print( "The post order is: ", + postorder(tree))
    print( "The post order is: ", + preorder(tree))
    print( "The post order is: ", + inorder(tree))

main()

这是错误:

Please enter the problem you wished parsed.(NOTE: problem must have parenthesis to seperate each binary grouping and must be spaced out.) ( 1 + 2 )
Traceback (most recent call last):
  File "C:\Users\Kevin\Desktop\Python Stuff\Assignment 11\parseTree.py", line 108, in 
    main()
  File "C:\Users\Kevin\Desktop\Python Stuff\Assignment 11\parseTree.py", line 102, in main
    tree = buildParseTree(parseData)
  File "C:\Users\Kevin\Desktop\Python Stuff\Assignment 11\parseTree.py", line 46, in buildParseTree
    currentTree = currentTree.getLeftChild()
  File "C:\Users\Kevin\Desktop\Python Stuff\Assignment 11\parseTree.py", line 15, in getLeftChild
    return root[1]
AttributeError: BinaryTree instance has no attribute '__getitem__'

3 个答案:

答案 0 :(得分:22)

因为你声明了你的方法错误:

让我们来看看如果你致电tree.getRootVal()会发生什么。 .getRootVal()以这种方式声明:

def getRootVal(root):
    return root[0]

您可能知道,传递给方法的第一个参数始终是实例,并且它是隐式提供的。因此,您基本上尝试将BinaryTree的实例视为序列(root[0])。

你必须这样指定:

class BinaryTree:
    def __init__(self,rootObj):
        self.key = rootObj
        self.left = None
        self.right = None
        self.root = [self.key, self.left, self.right]   # self.root

    def getRootVal(self):
        return self.root[0]   # access self.root

    def setRootVal(self, newVal):
        self.root[0] = newVal

    # and also the other functions

对象方法的第一个参数不必称为self。但这样做有助于避免像你那样的错误。

您有兴趣正确宣布insertLeftinsertRight;)

答案 1 :(得分:1)

您的首要问题是root应为self.root

你的第二个问题是:

def getLeftChild(root):
    return root[1]

您正在重新定义具有新含义的root。

答案 2 :(得分:0)

正如PreludeAndFugue所说,你应该修改格式。但是从我可以收集的内容来看,BinaryTree中的类方法中至少存在一个错误:它们中的大多数甚至不将self作为参数。解决这个问题,也许你可能会有点好转。