繁体   English   中英

定义类函数后,为什么我的类方法出现“未定义的函数”?

[英]Why do I get “function not defined” on my class method, when it is defined?

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 

类中定义的功能需要使用self来引用。 当调用函数时,虽然不需要self

new = self._insert_in_order(temp)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM