简体   繁体   English

名称错误:“self”未定义 - 调用 function 创建类内变量时

[英]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.

相关问题 Python错误“NameError:全局名称&#39;self&#39;未定义”在同一个类中调用另一个方法时 - Python error “NameError: global name 'self' is not defined” when calling another method in same class 为什么在类中调用另一个函数时出现“未定义名称”错误消息? - Why do I get a “Name is not defined” error message when calling another function within a class? NameError:在 class 中调用 function 时未定义名称 - NameError: name not defined when calling function inside of a class 使用类变量时,“ global”名称未定义错误 - `global` name not defined error when using class variables NameError:名称“ cost”未定义-调用函数时 - NameError: name 'cost' is not defined – when calling function 生成“名称”自身”的类中的类未定义”错误 - Class within a class generating 'name 'self' is not defined' error Python - 从类__init__方法调用函数时未定义全局名称 - Python - global name is not defined when calling function from class __init__ method 构造 Class 的规范方法(并避免“名称'self'未定义”)错误 - The canonical way to structure a Class (and to avoid the "name 'self' is not defined") error NameError:使用方法构建 Class 时未定义名称“self” - NameError: name 'self' is not defined when building Class with methods 定义名称时名称错误? 对于 class 项目 - Name error when name is defined? For class project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM