错误“NameError: name 'self' is not defined”即使我声明了“self”

黎英东

我正在用 Python 从头开始​​编写 AdaBoost。您能否详细说明该行self.functions[0] = f_0导致错误的原因?

class AdaBoost_regressor():
    def __init__(self, n_estimators, functions):
        # n_estimators is the number of weak regressors     
        self.n_estimators = n_estimators
        
        # We will store the sequence of functions in object "functions"
        self.functions = np.array([None] * n_estimators, dtype = 'f')
    
    # We set f_0 = 0
    def f_0(x):
        return 0
    self.functions[0] = f_0

结果是NameError: name 'self' is not defined

阿米特指南

我认为你出错的原因是你不能self在方法之外的类中使用,因为为了使用self类的实例必须作为参数传递给某个函数。

请注意,在初始化类之前,表达式没有任何意义self

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

不明白这个“NameError: name 'self' is not defined”错误

为什么我会收到“NameError: name 'self' is not defined”?

为什么我会收到此错误“NameError:name 'self' 未定义。”

NameError: name 'self' 未定义解释

raise self._value NameError: name 'global_df' is not defined

我有一个错误:NameError: name 'GameDisplay' is not defined

如何解决错误 NameError: name 'SparkConf' is not defined in pycharm

即使声明了字段,self.cleaned_data.get也不返回

我的代码中出现错误“NameError: name 'tweets_df' is not defined”

NameError:名称“ self”未定义

类型错误:get_first_name() 缺少 1 个必需的位置参数:Python 中的“self”

为什么会出现 NameError 的错误:name 'file' is not defined in python 3

self.table_name不起作用

GCP 中的 Terraform `name` 与 `self_link`

Self-assigning to a column name contained in a variable

为什么我收到错误“NameError: name 'kh' 未定义”

NameError: name 'char' 未定义错误

“使用未定义类型”错误即使我已经预先声明了类型

Pandas Dataframe NameError:我可以打印数据帧,但是当我尝试聚合列时,我得到 name '' is not defined 错误

在 leetcode 中,为什么我收到错误“NameError: global name 'climbStairs' is not defined”?但在没有“自我”和“对象”的情况下工作?

为什么我在结构体中调用self.init之前得到错误“ self”?

递归函数体内未定义的`self`不报告NameError

snakemake.remote.EGA:NameError:未定义名称“self”

Django在self.object中使用related_name

Python self [name] = value是什么意思?

遍历“ self”函数中的表,例如obj:function_name

导入 kafka 给出“NameError: name 'true' is not defined”

NameError: ("name 'true' is not defined", 'occurred at index 0')

如何修复“NameError: name 'context' is not defined”