[英]How to fix: "AttributeError: 'Class' object has no attribute 'rect'"
[英]How to fix: AttributeError:Class object has no attribute
我正在设置一个类,并且第一步已使用__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
,问题仍然存在
您的构造函数中有额外的下划线,因此不会被调用。 在两侧使用两个: __init__
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.