vendredi 25 décembre 2020

Python class object is not callable

I'm trying to run many tests in a code that I made for test the concept of linked lists. In the first test, I'm getting this error:

Traceback (most recent call last):
  File "LinkedList_test.py", line 14, in test_insert_first_node_to_head
    self.assertEqual('head', self.linked_list.head().value)
TypeError: 'Node' object is not callable

The LinkedList code:

class Node():
    def __init__(self, value = None):
        self.value = value
        self.next = None
        self.previous = None

class LinkedList():
    def __init__(self, head = None):
        self.head = head

    def append_node_to_tail(self, new_element):
        current = self.head
        if self.head:
            while current.next:
                current.next.previous = current
                current = current.next                    
            current.next = new_element
        else:
            self.head = new_element
               
    def tail():
        current = self.head
        while current.next:
            current = current.next
        return current

   ...other methods

The test code:

import unittest
from LinkedList import LinkedList, Node

class TestLinkedList(unittest.TestCase):
    def setUp(self):
        self.linked_list = LinkedList()

    def test_insert_first_node_to_tail(self):
        self.linked_list.append_node_to_tail(Node('head'))
        self.assertEqual('tail', self.linked_list.tail().value)

   ...other tests
if __name__ == '__main__':
    unittest.main()

Someone know what I'm doing wrong?

Aucun commentaire:

Enregistrer un commentaire