Search code examples
python-3.xlinked-listattributeerror

AttributeError: 'builtin_function_or_method' object has no attribute 'data'


I was trying to insert a node in a sorted linked list and while traversing it after insertion it is showing attribute error. It is inserting the node and traversing also but at the end it is throwing the error. Can somebody please explain what's wrong here?

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

Solution

  • 'builtin_function_or_method' object has no attribute 'x'
    

    usually means you forgot to add () to a function call, for example

    >>> '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'
    

    although your code seems right, I think whatever's calling traversal and insertNode is trying to pass the result of a function as an argument, but it's instead passing the function(it's missing the ())