Skip to main content
 首页 » 编程设计

python之如何修复 : AttributeError:Class object has no attribute

2025年05月04日18开发

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

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

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

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