简体   繁体   English

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

[英]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__'

Because you declared your methods wrong: 因为你声明你的方法错了:

Lets have a look what happens if you call tree.getRootVal() . 让我们看一下如果调用tree.getRootVal()会发生什么。 .getRootVal() is declared this way: .getRootVal()以这种方式声明:

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] ). 因此,您基本上尝试将BinaryTree的实例视为序列( 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 . 对象方法的第一个参数不必称为self But helps to do it this way in order to avoid errors like you did. 但有助于这样做,以避免像你一样的错误。

Interesting that you declared insertLeft and insertRight correctly ;) 有趣的是你正确地声明了insertLeftinsertRight ;)

Your first problem is that root should be self.root . 你的第一个问题是root应该是self.root

Your second problem is that here: 你的第二个问题是:

def getLeftChild(root):
    return root[1]

You are redefining root with a new meaning. 您正在重新定义具有新含义的root。

As PreludeAndFugue says, you should fix the formatting. 正如PreludeAndFugue所说,你应该修复格式。 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. 但是从我可以收集的内容来看,BinaryTree中的类方法中至少存在一个错误:它们中的大多数甚至不将self作为参数。 Fix that and maybe you might be a bit better off. 解决这个问题,也许你可能会有所改善。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我收到错误消息:“类型”对象没有属性“ __getitem__” - I am getting an error : 'type' object has no attribute '__getitem__' 为什么字典中没有属性'__getitem__'错误? - Why am I getting no attribute '__getitem__' error for dictionary? 虽然抓取错误实例方法没有属性'__getitem__' - While scraping getting error instance method has no attribute '__getitem__' 为什么我得到AttributeError:__getitem__ - why am i getting a AttributeError: __getitem__ 为什么我得到AttributeError:__ getitem__ - Why I am getting AttributeError: __getitem__ 在python-opencv中打印读取图像的像素值时出现错误,TypeError:'NoneType'对象没有属性'__getitem__' - I am getting error when printing the pixel value of the read image in python-opencv, TypeError: 'NoneType' object has no attribute '__getitem__' AttributeError:加密实例没有属性'__getitem__' - AttributeError: Encryption instance has no attribute '__getitem__' AttributeError:ObjectT实例没有属性'__getitem__' - AttributeError: ObjectT instance has no attribute '__getitem__' 对象没有属性'__getitem__'(类实例?) - Object has no attribute '__getitem__' (class instance?) 粒子实例没有属性'__getitem__' - Particle instance has no attribute '__getitem__'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM