为什么我得到"功能未定义"在我的类方法上,何时定义?

时间:2018-01-27 05:39:16

标签: python class methods

Wing IDE一直说明

line 403, in <module>
    new = _insert_in_order(self, temp)
builtins.NameError: name '_insert_in_order' is not defined 

但是,它在我的代码中定义。我的老师没有回复,我被困住了。有关如何处理它的任何建议?问题在于添加功能。

class SortedFreqList(FreqList):
"""FreqList that keeps items in order, sorted by their frequencies"""

    def __init__(self):
        FreqList.__init__(self)
        self.freq_list_type = 'SortedFreqList'

    def _insert_in_order(self, freq_node):
        """ Takes a FreqNode and inserts in to the list so that
        items are sorted from largest to smallest.
        NOTE: The list must contain something for this method to work.
        In general this method should only be called from the add method,
        see the add method docstring for information on how to use this method.
        ***** DON'T change this method *****
        """
        # so we don't have to lookup each time
        freq_of_item = freq_node.frequency

        # check to see if larger than first freq in list
        if freq_of_item > self.head.frequency:
            freq_node.next_node = self.head
            self.head = freq_node
        else:
            curr_freq = self.head
            inserted = False
            while curr_freq.next_node is not None and not inserted:
                if freq_of_item > curr_freq.next_node.frequency:
                    # insert here
                    freq_node.next_node = curr_freq.next_node
                    curr_freq.next_node = freq_node
                    inserted = True
                else:
                    curr_freq = curr_freq.next_node
            # got to end and didn't find
            if not inserted:
                freq_node.next_node = None  # as now at end of list
                curr_freq.next_node = freq_node

    def add(self, new_item):
        """
        If the list is empty then make a new FreqNode and insert it at head.
        If the new_item is not already in freq list then adds the given
        item with a frequency of 1 as a FreqNode object to the end of the list.

        If the given new item is already in the list,
           the frequency is incremented by 1.
           If needed (ie, the freq is now greater than the previous node),
             the node is removed and then inserted
             in to its sorted position - using _insert_in_order.

        >>> f = SortedFreqList()
        >>> f.add('a')
        >>> print(f)
        Sorted Frequency List
        ---------------------
          1:  'a' = 1
        >>> f.add('b')
        >>> print(f)
        Sorted Frequency List
        ---------------------
          1:  'a' = 1
          2:  'b' = 1
        >>> f.add('b')
        >>> print(f)
        Sorted Frequency List
        ---------------------
          1:  'b' = 2
          2:  'a' = 1
        >>> f.add('c')
        >>> print(f)
        Sorted Frequency List
        ---------------------
          1:  'b' = 2
          2:  'a' = 1
          3:  'c' = 1
        >>> f.add('a')
        >>> print(f)
        Sorted Frequency List
        ---------------------
          1:  'b' = 2
          2:  'a' = 2
          3:  'c' = 1
        >>> f.add('c')
        >>> print(f)
        Sorted Frequency List
        ---------------------
          1:  'b' = 2
          2:  'a' = 2
          3:  'c' = 2
        >>> f.add('c')
        >>> f.add('d')
        >>> f.add('d')
        >>> f.add('e')
        >>> print(f)
        Sorted Frequency List
        ---------------------
          1:  'c' = 3
          2:  'b' = 2
          3:  'a' = 2
          4:  'd' = 2
          5:  'e' = 1
        >>> f.add('e')
        >>> f.add('e')
        >>> print(f)
        Sorted Frequency List
        ---------------------
          1:  'c' = 3
          2:  'e' = 3
          3:  'b' = 2
          4:  'a' = 2
          5:  'd' = 2
        """
        # make sure you read the docstring for this method!
        # ---start student section---
        if self.head is None:
            self.head = FreqNode(new_item)
        else:
            found = False
            current = self.head
            previous = None
            while current is not None:
                if current.item == new_item:
                    current.increment()
                    found = True
                    temp = current
                    previous.next_node = current.next_node
                    new = _insert_in_order(self, temp)
                previous = current
                current = current.next_node

            if not found:
                new_node = FreqNode(new_item)
                current = self.head
                while current.next_node!= None:
                    current = current.next_node
                current.next_node = new_node 

2 个答案:

答案 0 :(得分:1)

类中定义的函数需要使用self引用。在调用该功能时,您不需要self

new = self._insert_in_order(temp)

答案 1 :(得分:1)

您的方法__init__insert_in_order没有缩进,因此它们实际上并不是SortedFreqList类的一部分。 (编辑:你修好了)

首先修复缩进,重新运行,确认错误消失。 (在内部方法中,您还需要使用self.insert_in_order调用其他方法,如Idlehands所说)