[英]Name error: 'self' not defined - when calling a function to create in-class variables
I have the following class:我有以下 class:
class Documents:
def __init__(self, input_file):
self.input_file_ = input_file #List in which each element is a list of tokens
assert type(self.input_file_) is list, 'Input file is not a list'
assert type(self.input_file_[0]) is list, 'Elements in input file are not lists' #Only checks first instance, not all. But should suffice
def get_vocabulary(self):
vocabulary = set([el for lis in self.input_file_ for el in lis])
return vocabulary, len(vocabulary)
vocabulary, vocabulary_size = self.get_vocabulary()
But when I try to execute it, I get the following error:但是当我尝试执行它时,我收到以下错误:
Traceback (most recent call last):
File "<ipython-input-34-4268f473c299>", line 1, in <module>
class Documents:
File "<ipython-input-34-4268f473c299>", line 30, in Documents
vocabulary, vocabulary_size = self.get_vocabulary()
NameError: name 'self' is not defined
This is a common error on SO.这是 SO 上的常见错误。 However, I have not found an answer in which the code has a similar structure.
但是,我还没有找到代码具有类似结构的答案。
Can someone explain to me why I get this error and how I can change my code so that I don't get an error?有人可以向我解释为什么会出现此错误以及如何更改代码以免出现错误吗?
The way you have it, vocabulary, vocabulary_size = self.get_vocabulary()
is being executed when the class is being defined , so there is no self
.当 class 被定义时,你拥有它的方式,
vocabulary, vocabulary_size = self.get_vocabulary()
正在执行,所以没有self
。 The latter is the name of the first argument passed to methods of a class and is the instance of the class (that was previously created) on which to operate.后者是传递给 class 方法的第一个参数的名称,并且是要对其进行操作的 class(先前创建的)的实例。
The proper way to do this would be to call get_vocabulary()
method from the __init__()
method when an instance of the class exists and is being initialized.正确的方法是在 class 的实例存在并且正在初始化时从
__init__()
方法调用get_vocabulary()
方法。
Here's what I mean:这就是我的意思:
class Documents:
def __init__(self, input_file):
self.input_file_ = input_file # List in which each element is a list of tokens
self.vocabulary, self.vocabulary_size = self.get_vocabulary()
assert type(self.input_file_) is list, 'Input file is not a list'
assert type(self.input_file_[0]) is list, 'Elements in input file are not lists' # Only checks first instance, not all. But should suffice
def get_vocabulary(self):
vocabulary = set([el for lis in self.input_file_ for el in lis])
return vocabulary, len(vocabulary)
Comment (off-topic):评论(题外话):
In languages that have classes and support object-orientated code like Python, it's usually best to avoid type-checking as much as possible because it doesn't support subtyping — but you can overcome that limitation when it is done by using the built-in isinstance()
built-in function.在具有类并支持面向对象代码(如 Python)的语言中,通常最好尽可能避免类型检查,因为它不支持子类型化——但您可以通过使用内置的
isinstance()
内置 function。
This implies that it would probably be better to do the following in your __init__()
method:这意味着在
__init__()
方法中执行以下操作可能会更好:
assert isinstance(self.input_file, list), 'Input file is not a list'
assert isinstance(self.input_file_[0], list), 'Elements in input file are not lists' # Only checks first instance, not all. But should suffice
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.