小编Tie*_*ruc的帖子

如何修复:AttributeError:Class对象没有属性

我正在设置一个类,并且第一步使用__init__函数来初始化属性。但是,当我尝试从该类创建实例时,它显示AttributeError

我已经一遍又一遍地检查代码,看语法是否有问题,但错误仍然存​​在

class RandomWalk():
    def ___init___(self, points = 10):
        """initialize attributes of a walk"""
        self.points = points
        self.x_values = [0]
        self.y_values = [0]

rw = RandomWalk()
print(rw.points)
Run Code Online (Sandbox Code Playgroud)

我期望输出10作为默认值points,但错误显示:

Traceback (most recent call last):
  File "test1.py", line 10, in <module>
    print(rw.points)
AttributeError: 'RandomWalk' object has no attribute 'points'
Run Code Online (Sandbox Code Playgroud)

如果我用或替换属性points,问题仍然存在x_valuesy_values

python attributeerror

1
推荐指数
1
解决办法
34
查看次数

标签 统计

attributeerror ×1

python ×1