Python AttributeError对象属性是只读的

时间:2013-11-13 08:49:00

标签: python attributes

  File "C:\Users\USER\Desktop\myList2.py", line 44, in mkListNode
    listNode.next = next
AttributeError: 'ListNode' object attribute 'next' is read-only

我不知道这意味着什么。下面是myList.py的完整代码,任何帮助都会很棒!感谢

class ListNode():
    """
    All true value-containing wheel nodes are represented as
    instances of ListNode.
    """
    __slots__ = ('data', 'next', 'prev')

class MyList():
    """
    The container for a linked wheel class.
    It contains a reference to a cursor in the wheel.
    Invariant: size==0 iff type(cursor)==EmptyListNode
    """
    __slots__ = ('cursor', 'size')

def mkListNode(data, next, prev):
    """
    Make a new list node
    Returns a new node
    """
    listNode = ListNode()
    listNode.data = data
    listNode.next = next
    listNode.prev = prev
    return ListNode

def add(myList, element):
    """
    add: Add a node to the wheel right after the cursor
    Effect: A new node is in the wheel just after the cursor
    """
    myList.size += 1
    if myList.cursor == EmptyListNode:
        myList.cursor = mkListNode(EmptyListNode, element, EmptyListNode)
        myList.cursor.prev = myList.cursor
        myList.cursor.next = myList.cursor
    else:
        temp = mkListNode(myList.cursor.prev, element, myList.cursor.next)
        myList.cursor.prev.next = temp
        myList.cursor.prev = temp

我不明白为什么ListNode只是只读的?嗯,这很有趣,永远不会播放关于对象属性的只读。

1 个答案:

答案 0 :(得分:1)

接下来是Python内置词。尝试在另一个变量旁边进行更改。