简体   繁体   English

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

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

Wing IDE keeps stating that Wing IDE不断指出

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

however, it is defined in my code. 但是,它在我的代码中定义。 My teacher is not replying and I am stuck. 我的老师没有回复,我被卡住了。 Any advices on how I could approach it? 关于如何处理的任何建议? The problem is in the add function. 问题出在添加功能上。

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 

Functions defined in a class needs to be referenced with self . 类中定义的功能需要使用self来引用。 When calling the function though you don't need the self . 当调用函数时,虽然不需要self

new = self._insert_in_order(temp)

Your methods __init__ and insert_in_order are not indented, hence they're not actually part of SortedFreqList class. 您的方法__init__insert_in_order不缩进,因此它们实际上不是SortedFreqList类的一部分。 (EDIT: you fixed that) (编辑:你解决了)

Fix that indentation first, rerun, confirm the error disappears. 首先修复该缩进,然后重新运行,确认错误消失。 (Inside methods, you also need to call other methods with self.insert_in_order as Idlehands said) (在内部方法中,您还需要使用self.insert_in_order所说的self.insert_in_order调用其他方法)

暂无
暂无

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

相关问题 为什么在类中调用另一个函数时出现“未定义名称”错误消息? - Why do I get a “Name is not defined” error message when calling another function within a class? 当我在另一个函数上定义它时,为什么我得到名称未定义的错误? - Why do i get Name is not defined error for python when i defined it on another function? 获取 class 定义的方法 - Get class that defined method 为什么在上述类中定义TwitterClient时,为什么不断收到93行错误,提示未定义TwitterClient? - Why do I keep getting a line 93 error saying TwitterClient not defined when it is defined in a class above? 为什么我的功能未定义 - Why is my function not defined 为什么我的函数未定义? - Why my function is not defined? 为什么我的 function 在我明确定义为其他方式时返回 None ? 这是用于计算形状面积的基本 function - Why is my function returning None when I clearly have defined it to do otherwise? This is a basic function for calculating the area of shapes 为什么在python中出现错误,提示即使我先前定义了方法也找不到方法? - Why do I get an error in python saying that a method cannot be found even though I previously defined the method? 为什么我定义的“ Dog”类的“ walk”方法不能被“ Bulldog”或“ Russell Terrier”子类继承? - Why doesn't my defined 'walk' method of the 'Dog' class get inherited by the 'Bulldog' or 'Russell Terrier' child classes? 当我有多个对象时,如何间接引用类中定义的方法 - How do I refer indirectly to a method defined in a class when I have multiple ojects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM