Kevin Yusko
Kevin Yusko

Reputation: 111

Why am I getting a " instance has no attribute '__getitem__' " error?

Here's the code:

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()

And here is the error:

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__'

Upvotes: 11

Views: 68407

Answers (3)

Felix Kling
Felix Kling

Reputation: 816790

Because you declared your methods wrong:

Lets have a look what happens if you call tree.getRootVal(). .getRootVal() is declared this way:

def getRootVal(root):
    return root[0]

As you probably know, the first parameter passed to a method is always the instance and it is provided implicitly. So you basically try to treat an instance of BinaryTree as a sequence(root[0]).

You have to specify it this way:

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

The first parameter to an objects method does not have to be called self. But helps to do it this way in order to avoid errors like you did.

Interesting that you declared insertLeft and insertRight correctly ;)

Upvotes: 22

inspectorG4dget
inspectorG4dget

Reputation: 114025

As PreludeAndFugue says, you should fix the formatting. But from what I can gather, there is at least one error in your class methods in BinaryTree: most of them don't even take self as a parameter. Fix that and maybe you might be a bit better off.

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838786

Your first problem is that root should be self.root.

Your second problem is that here:

def getLeftChild(root):
    return root[1]

You are redefining root with a new meaning.

Upvotes: 1

Related Questions