Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.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类的问题_Python - Fatal编程技术网

python类的问题

python类的问题,python,Python,嗨,我是Python新手,作为我作业的一部分,我编写了以下课程 import nltk.stem.api class BanglaStemmer(nltk.stem.api.StemmerI): suffixList = ['\xef\xbb\xbf\xe0\xa6\xbf\xe0\xa7\x9f\xe0\xa7\x87\xe0\xa6\x9b\xe0\xa6\xbf\xe0\xa6\xb2\xe0\xa6\xbe\xe0\xa6\xae\n', '\xe0\xa6\xbf\xe0\

嗨,我是Python新手,作为我作业的一部分,我编写了以下课程

import nltk.stem.api

class BanglaStemmer(nltk.stem.api.StemmerI):

    suffixList = ['\xef\xbb\xbf\xe0\xa6\xbf\xe0\xa7\x9f\xe0\xa7\x87\xe0\xa6\x9b\xe0\xa6\xbf\xe0\xa6\xb2\xe0\xa6\xbe\xe0\xa6\xae\n', '\xe0\xa6\xbf\xe0\xa7\x9f\xe0\xa7\x87\xe0\xa6\x9b\xe0\xa6\xbf\xe0\xa6\xb2\xe0\xa7\x87\xe0\xa6\xa8\n', '\xe0\xa6\xbf\xe0\xa7\x9f\xe0\xa7\x87\xe0\xa6\x9b\xe0\xa6\xbf\xe0\xa6\xb2\xe0\xa7\x87\n', '\xe0\xa6\xbf\x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b\xe0\xa6\xbf\n', '\xe0\xa6\x9b\xe0\xa7\x87\xe0\xa6\xa8\n', '\xe0\xa6\x9b\n', '\xe0\xa6\xa4\xe0\xa6\xbf\xe0\xa6\xb8\n', '\xe0\xa6\xa4\xe0\xa6\xbe\xe0\xa6\xae\n', '\xe0\xa6\xb2\xe0\xa6\xbe\xe0\xa6\xae\n', '\xe0\xa6\xb2\xe0\xa7\x87\xe0\xa6\xa8\n', '\xe0\xa6\xa4\xe0\xa7\x87\xe0\xa6\xa8\n', '\xe0\xa6\xac\xe0\xa7\x87\xe0\xa6\xa8\n', '\xe0\xa7\x87\xe0\xa6\xa8\n', '\xe0\xa6\xbf\xe0\xa6\xb8\n', '\xe0\xa7\x81\xe0\xa6\xa8\n', '\xe0\xa7\x81\xe0\xa6\x95\n', '\xe0\xa6\xb2\xe0\xa7\x87\n', '\xe0\xa6\xac\xe0\xa7\x87\n', '\xe0\xa6\xb2\xe0\xa6\xbf\n', '\xe0\xa6\xac\xe0\xa6\xbf\n', '\xe0\xa6\xa4\xe0\xa6\xbf\n', '\xe0\xa6\xb2\n', '\xe0\xa6\xa4\n', '\xe0\xa7\x8b\n', '\xe0\xa6\xbf\n', '\xe0\xa7\x87\n', '\xe0\xa7\x8d\n', '\xe0\xa6\x87\n', '\xe0\xa6\xac\n', '\xe0\xa6\xb8\n', '\xe0\xa6\xa8\n', '\xe0\xa6\x95\n', '\xe0\xa6\x93\n', '\xe0\xa7\x9f\n']

    def stem(self,token):
        for suffix in suffixList:
            if token.endswith(suffix):
                return token[:-len(suffix)]

        return token

问题是,当我试图通过创建一个实例并调用带有参数的
stem()
函数来编译和运行它时,它会说
subfixlist
没有定义。我想不出是什么问题。类变量的声明方式是否不同?请提供帮助

您可以使用
BanglaStemmer.subfixlist
self.subfixlist
访问变量。普通
后缀列表
仅适用于局部(在函数中定义)或全局(在模块中定义)变量

应该是

for suffix in self.suffixList:

与许多OOP语言不同,Python要求您在类方法中限定类成员。

在Python中,与大多数其他语言不同,您始终必须明确引用要访问其成员的实例(或Python中的“属性”)。此实例始终作为第一个参数传递给方法;这就是为什么第一个参数按惯例称为
self

subfixlist
在您的例子中是一个类属性,即它由类的所有实例共享(除非重新定义)。因此,您可以使用
self.supplexlist
访问它

for suffix in self.suffixList: