2010-04-01 64 views
0

您好我是新来的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\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\xe0\xa7\x9f\xe0\xa7\x87\xe0\xa6\x9b\xe0\xa6\xbf\xe0\xa6\xb2\xe0\xa6\xbf\n', '\xe0\xa6\xbf\xe0\xa7\x9f\xe0\xa7\x87\xe0\xa6\x9b\xe0\xa6\xbf\xe0\xa6\xb2\n', '\xe0\xa6\xbf\xe0\xa7\x9f\xe0\xa7\x87\xe0\xa6\x9b\xe0\xa7\x87\xe0\xa6\xa8\n', '\xe0\xa6\xbf\xe0\xa7\x9f\xe0\xa7\x87\xe0\xa6\x9b\xe0\xa6\xbf\xe0\xa6\xb8\n', '\xe0\xa6\xbf\xe0\xa7\x9f\xe0\xa7\x87\xe0\xa6\x9b\xe0\xa6\xbf\n', '\xe0\xa6\xbf\xe0\xa7\x9f\xe0\xa7\x87\xe0\xa6\x9b\xe0\xa7\x87\n', '\xe0\xa6\xbf\xe0\xa7\x9f\xe0\xa7\x87\xe0\xa6\x9b\n', '\xe0\xa6\xbf\xe0\xa7\x9f\xe0\xa7\x8b\n', '\xe0\xa7\x87\xe0\xa6\x9b\xe0\xa6\xbf\xe0\xa6\xb2\xe0\xa6\xbe\xe0\xa6\xae\n', '\xe0\xa7\x87\xe0\xa6\x9b\xe0\xa6\xbf\xe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\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()函数带有一个参数,它表示没有定义suffixList。无法弄清楚有什么问题。是否有不同的方式来声明类变量?请帮助

回答

6
for suffix in suffixList: 

应该

for suffix in self.suffixList: 

不像很多OOP语言,Python的需要你有资格类的方法里面类成员。

2

您可以使用BanglaStemmer.suffixListself.suffixList来访问变量。普通suffixList仅适用于本地(在函数中定义)或全局(在模块中定义)变量。

0

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

suffixList在你的情况下是一个类属性,即它被类的所有实例共享(除非重新定义)。所以你可以用self.suffixList来访问它。