这个有序清单有什么问题? AttributeError:'function'对象没有属性'data'

时间:2021-09-21 17:13:47

I'm following the tutorial from Interactive Python to make an Ordered List in Python. My code looks like this:

我正在按照Interactive Python的教程在Python中创建一个Ordered List。我的代码如下所示:

class Node:
    def __init__(self, initdata):
        self.data = initdata
        self.next = None

    def getData(self):
        return self.data

    def getNext(self):
        return self.next

    def setData(self, newdata):
        self.data = newdata

    def setNext(self, newnext):
        self.next = newnext

class OrderedList:
    def __init__(self):
        self.head = None

    def search(self, item):
        found = False
        stop = False
        current = self.head
        while current != None and not found and not stop:
            if current.getData() == item:
                found = True
            elif current.getData() > item:
                stop = True
            else:
                current = current.getNext()

        return found

    def add(self, item):
        previous = None
        current = self.head
        stop = False
        while current != None and not stop:
            if current.getData() > item:
                stop = True
            else:
                previous = current
                current = current.getNext()

        temp = Node(item)       
        if previous == None:
            temp.setNext(self.head)
            self.head = temp
        else:
            temp.setNext(current)
            previous.setNext(temp)

    def remove(self, item):
        previous = None
        current = self.head
        found = False
        while not found:
            if current.getData() == item:
                found = True
            else:
                previous = current
                current = current.getNext()

        if previous == None:
            self.head = current.getNext()
        else:
            previous.setNext(current.getNext)

    def update(self, olddata, newdata):
        self.remove(olddata)
        self.add(newdata)

    def display(self):
        current = self.head
        print("The contents of this list are: ", end='')
        print()
        while current:
            print(current.data)
            current = current.getNext()

myList = OrderedList()
myList.add(5)
myList.add(25)
myList.add(30)
myList.remove(25)
myList.display()
myList.update(5, 30)
myList.display()

It keeps telling me my variable current is a function and doesn't have the atrributes for a Node. How can I fix it and what am I doing wrong?

它一直告诉我,我的变量current是一个函数,并没有Node的atrributes。我该如何解决它,我做错了什么?

The output I get:

我得到的输出:

The contents of this list are: 
5
Traceback (most recent call last):
  File "<stdin>", line 92, in <module>
  File "<string>", line 88, in <module>
  File "<string>", line 80, in display
AttributeError: 'function' object has no attribute 'data'

2 个解决方案

#1


1  

You assigned the Node.getNext method to your Node.next attribute in your OrderedList.remove method:

您已将Node.getNext方法分配给OrderedList.remove方法中的Node.next属性:

else:
    previous.setNext(current.getNext)

You can see this by introspecting your myList.head attribute:

您可以通过内省myList.head属性来查看:

>>> vars(myList.head)
{'data': 5, 'next': <bound method Node.getNext of <__main__.Node object at 0x105a726d8>>}

You wanted to call the method there; otherwise you'll end up with Node.getNext() returning that method references rather than a Node() instance.

你想在那里调用这个方法;否则你最终将返回Node.getNext()返回该方法引用而不是Node()实例。

Fixing that mistake, your code appears to work:

修复该错误,您的代码似乎有效:

The contents of this list are: 
5
30
The contents of this list are: 
30
30

#2


0  

I think you got the recursive structure of this so-constructed ordered list. So, this is maybe just a typo - correct it like this:

我认为你得到了这个如此构造的有序列表的递归结构。所以,这可能只是一个错字 - 纠正它像这样:

class OrderedList:
    def __init__(self):
        self.head = Node(None)

then there is one more typo left, that can be easily found.

然后还有一个错字,可以很容易找到。

Code snipped to comment below:

代码剪断以下评论:

def remove(self, item):
    previous = None
    current = self.head
    found = False
    while not found:
        if current.getData() == item:
            found = True
        else:
            previous = current
            current = current.getNext()
            if current == None: 
                print "Element not found"
                return
    ...

#1


1  

You assigned the Node.getNext method to your Node.next attribute in your OrderedList.remove method:

您已将Node.getNext方法分配给OrderedList.remove方法中的Node.next属性:

else:
    previous.setNext(current.getNext)

You can see this by introspecting your myList.head attribute:

您可以通过内省myList.head属性来查看:

>>> vars(myList.head)
{'data': 5, 'next': <bound method Node.getNext of <__main__.Node object at 0x105a726d8>>}

You wanted to call the method there; otherwise you'll end up with Node.getNext() returning that method references rather than a Node() instance.

你想在那里调用这个方法;否则你最终将返回Node.getNext()返回该方法引用而不是Node()实例。

Fixing that mistake, your code appears to work:

修复该错误,您的代码似乎有效:

The contents of this list are: 
5
30
The contents of this list are: 
30
30

#2


0  

I think you got the recursive structure of this so-constructed ordered list. So, this is maybe just a typo - correct it like this:

我认为你得到了这个如此构造的有序列表的递归结构。所以,这可能只是一个错字 - 纠正它像这样:

class OrderedList:
    def __init__(self):
        self.head = Node(None)

then there is one more typo left, that can be easily found.

然后还有一个错字,可以很容易找到。

Code snipped to comment below:

代码剪断以下评论:

def remove(self, item):
    previous = None
    current = self.head
    found = False
    while not found:
        if current.getData() == item:
            found = True
        else:
            previous = current
            current = current.getNext()
            if current == None: 
                print "Element not found"
                return
    ...