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

Tie*_*ruc 1 python attributeerror

我正在设置一个类,并且第一步使用__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

Ben*_*Ben 5

您的构造函数中有额外的下划线,因此不会被调用。在两侧使用两个:__init__