类函数在Python中找不到位置参数Self

时间:2018-04-26 07:31:45

标签: python class linked-list

我在Python中创建了一个链表类,当调用我定义的size函数时,我收到以下错误:

TypeError: get_next() missing 1 required positional argument: 'self'

我尝试调用另一个我定义的函数,它也使用get_next()函数,但它没有产生错误。下面是类定义以及测试代码。

LinkedLists.py

class Node(object):
    def __init__(self, data = None, next_node = None):
        self.data = data
        self.next_node = next_node

    def get_data(self):
        return self.data

    def get_next(self):
        return self.next_node

    def set_next(self, new_next):
        self.next_node = new_next

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

    def insert(self, data):
        new_node = Node(data)

        new_node.set_next(self.head)
        self.head = new_node

    def size(self):
        current = self.head
        count = 0

        while current:
            count += 1
            current = current.get_next()

        return count

    def search(self, data):
        current = self.head
        found = False

        while current:
            if current.get_data() == data:
                found = True
            else:
                current = current.get_next()

        if current is None:
            raise ValueError("Data not in list")

        return current

    def delete(self, data):
        current = self.head
        previous = None
        found = False

        while current and found is False:
            if current.get_data() == data:
                found = True
            else:
                previous = current
                current = current.get_next()

        if current is None:
            raise ValueError("Data not in list")

        if previous is None:
            self.head = current.get_next()
        else:
            previous.set_next(current.get_next())

    def insert_at(self, data, location):
        new_node = Node(data)
        current = self.head
        found = False

        while current and found is False:
            if current.get_data() == data:
                found = True
            else:
                current = current.get_next()

            if current is None:
                raise ValueError("Data not in list")

            new_node.set_next(current.get_next())

            current.set_next(new_node)

LinkedListsTest.py

from LinkedLists import *

List = LinkedList()

List.insert(5)
List.insert(6)
List.insert(8)
List.delete(6)

print(List.size())

错误的完整回溯:

Traceback (most recent call last):
  File "LinkedListsTest.py", line 10, in <module>
    print(List.size())
  File ".../LinkedLists.py", line 31, in size
    current = current.get_next()
TypeError: get_next() missing 1 required positional argument: 'self'

2 个答案:

答案 0 :(得分:2)

您将self.head设置为Node ,而不是实例:

def __init__(self, head = Node):
    self.head = head

请注意那里的Node引用。 Node.get_next()调用未绑定的方法,并且不传入self

但是,请勿将head=Node()设为默认值;默认值在函数定义时设置一次,并且可变默认值会导致问题,因为LinkedList类的所有实例将共享该实例。请参阅"Least Astonishment" and the Mutable Default Argument

使用None之类的标记来检测您是否需要创建默认值:

def __init__(self, head=None):
    if head is None:
        # create an empty default
        head = Node()
    self.head = head

通过此更正,您的测试将打印3

答案 1 :(得分:1)

假设我已正确解释了正确的缩进,我认为您的默认参数可能不正确:

class LinkedList(object):
    def __init__(self, head = Node()): # <== default should be instance
        self.head = head

但是,这会产生令人惊讶的结果。你可能意味着这个:

class LinkedList(object):
    def __init__(self, head = None):
        if head is None:
            head = Node()
        self.head = head