Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python编程:全局名称';在'之内;没有定义。I';我正在使用列表过滤器 class单词列表: def在(word)中: 返回3_Python_List_Filter_Global_Defined - Fatal编程技术网

Python编程:全局名称';在'之内;没有定义。I';我正在使用列表过滤器 class单词列表: def在(word)中: 返回3

Python编程:全局名称';在'之内;没有定义。I';我正在使用列表过滤器 class单词列表: def在(word)中: 返回3,python,list,filter,global,defined,Python,List,Filter,Global,Defined,如果在类方法定义中调用类方法,则需要调用“self”(因此,在示例中self.is_in)。类方法的第一个参数也应该是“self”,它引用类的这个实例。请查看一个好的解释 Traceback (most recent call last): File "C:/Users/Makoy/Documents/AMC 125/sample.py", line 1, in <module> class wordlist: File "C:/Users/Makoy/Documen

如果在类方法定义中调用类方法,则需要调用“self”(因此,在示例中self.is_in)。类方法的第一个参数也应该是“self”,它引用类的这个实例。请查看一个好的解释

Traceback (most recent call last):
  File "C:/Users/Makoy/Documents/AMC 125/sample.py", line 1, in <module>
    class wordlist:
  File "C:/Users/Makoy/Documents/AMC 125/sample.py", line 6, in wordlist
    newWordlist = truncate_by_length(['mark', 'daniel', 'mateo', 'jison'])
  File "C:/Users/Makoy/Documents/AMC 125/sample.py", line 5, in truncate_by_length
    return filter(is_within, wordlist)
NameError: global name 'is_within' is not defined
class单词列表:
def在(self,word)中:

return 3虽然timc的答案解释了代码出现错误的原因以及如何修复错误,但类的当前设计相当糟糕。wordlist类只包含两个对外部数据进行操作的方法-通常不需要为此创建类,您可以直接在模块的全局范围内定义它们。wordlist类的更好设计如下:

class wordlist:
    def is_within(self, word):
        return 3 <= (len(word)) <= 5
    def truncate_by_length(self,wordlist):
        return filter(self.is_within, wordlist)

wl = wordlist()    
newWordList = wl.truncate_by_length(['mark', 'daniel', 'mateo', 'jison'])
print newWordList    
class wordlist():
    def __init__(self, wlist):
        #save the word list as an instance variable
        self._wlist = wlist

    def truncate_by_length(self):
        #truncante the word list using a list comprehension
        self._wlist = [word for word in self._wlist if 3 <= len(word) <= 5]

    def __str__(self):
        #string representation of the class is the word list as a string
        return str(self._wlist)

self
与其他变量一样,是一个普通变量;这只是惯例,它是方法的第一个参数的名称。他的方法没有
self
参数,因此他将在
self
上得到
namererror
。祝您好运!我只是照你说的做了,但仍然收到同样的错误。我错过什么了吗?就像icktoofay所说的那样。@icktoofay感谢您的澄清,您完全正确,我已将我的答案修改为such@MacMac请检查我的最新答案。您的代码有一些问题。首先,需要将“self”作为第一个参数添加到类方法函数中。接下来,在执行truncate_by_length方法之前,需要创建“wordlist”的实例。有一种更好的方法来构造这段代码,但希望这能帮助您理解这个问题。
class wordlist():
    def __init__(self, wlist):
        #save the word list as an instance variable
        self._wlist = wlist

    def truncate_by_length(self):
        #truncante the word list using a list comprehension
        self._wlist = [word for word in self._wlist if 3 <= len(word) <= 5]

    def __str__(self):
        #string representation of the class is the word list as a string
        return str(self._wlist)
w = wordlist(['mark', 'daniel', 'mateo', 'jison'])
w.truncate_by_length()
print w