[英]How to fix: AttributeError:Class object has no attribute
I am setting up a class, and as first step has used __init__
function to initialize the attributes. 我正在设置一个类,并且第一步已使用
__init__
函数初始化属性。 However when I try to create an instance from that class, it shows the AttributeError
. 但是,当我尝试从该类创建实例时,它将显示
AttributeError
。
I have checked the codes again and again to see if there is anything wrong with the syntax but the error remains 我已经一遍又一遍地检查代码,看语法是否有问题,但错误仍然存在
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)
I expect the output of 10 as default value of points
, but instead the error shows: 我期望输出10作为
points
默认值,但是错误显示:
Traceback (most recent call last):
File "test1.py", line 10, in <module>
print(rw.points)
AttributeError: 'RandomWalk' object has no attribute 'points'
The issue remains if I replace attribute points
with either x_values
or y_values
如果我用
x_values
或y_values
替换属性points
,问题仍然存在
You have extra underscores in your constructor, so it is not being called. 您的构造函数中有额外的下划线,因此不会被调用。 Use two on either side:
__init__
. 在两侧使用两个:
__init__
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.