【问题标题】:AttributeError: 'builtin_function_or_method' object has no attribute 'data'AttributeError:“builtin_function_or_method”对象没有属性“数据”
【发布时间】:2020-12-11 17:05:06
【问题描述】:

我试图在排序的链表中插入一个节点,并且在插入后遍历它时显示属性错误。它正在插入节点并遍历,但最后它抛出错误。有人可以解释一下这里出了什么问题吗?

def traversal(head):
    curNode = head
    while curNode is not None:
        print(curNode.data , end = '->')
        curNode = curNode.next
def insertNode(head,value):
    predNode = None
    curNode = head
    while curNode is not None and curNode.data < value:
        predNode = curNode
        curNode = curNode.next
    newNode = ListNode(value)
    newNode.next = curNode
    
    if curNode == head:
        head = newNode
    else:
        predNode.next = newNode
        
    return head

【问题讨论】:

  • 您提供的代码看起来不错。能否也提供ListNode的代码?

标签: python-3.x linked-list attributeerror


【解决方案1】:
'builtin_function_or_method' object has no attribute 'x'

通常意味着您忘记在函数调用中添加(),例如

>>> 'thing'.upper.replace("H", "") #WRONG
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'replace'

>>> "thing".upper().replace("H", "") #RIGHT
'TING'

虽然您的代码看起来是正确的,但我认为无论调用 traversal 和 insertNode 都试图将函数的结果作为参数传递,但实际上是在传递函数(它缺少 ())

【讨论】:

    猜你喜欢
    • 2018-11-29
    • 2018-03-27
    • 2020-11-27
    • 2016-01-31
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多