首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当我的类方法被定义时,为什么我得到的是“函数未定义”?

当我的类方法被定义时,为什么我得到的是“函数未定义”?
EN

Stack Overflow用户
提问于 2018-01-27 13:39:16
回答 2查看 687关注 0票数 0

Wing IDE一直在说

代码语言:javascript
复制
line 403, in <module>
    new = _insert_in_order(self, temp)
builtins.NameError: name '_insert_in_order' is not defined 

但是,它是在我的代码中定义的。我的老师没有回答,我被卡住了。有什么关于我如何处理它的建议吗?问题出在add函数中。

代码语言:javascript
复制
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 
EN

Stack Overflow用户

发布于 2018-01-27 13:48:49

您的方法__init__insert_in_order没有缩进,因此它们实际上不是SortedFreqList类的一部分。(编辑:您已修复此问题)

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

票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48472958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档