Python AttributeError 对象属性是只读的

标签 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 只是只读的?嗯,这很有趣,永远不要为对象属性设置只读。

最佳答案

next 是 Python 内置词。尝试在另一个变量旁边更改。

关于Python AttributeError 对象属性是只读的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19949239/

相关文章:

python - 拆分列中的值并创建新的 cols 小问题

python - 为什么我不能直接向任何 python 对象添加属性?

python - 如何为类的实例定义默认属性列表?

python - 从 fasta header 创建文本文件时遇到问题

python - XML 字符串到 Python 对象

python - Redis - 如何 RPUSH/LPUSH 一个空列表

python - 如何从文本文件中去除标点符号

javascript - 选择没有值属性的所有单选按钮的最安全方法

python - 无法在 "object"类的实例上设置属性

python - 如何在 Python 中为实例分配属性?