Python 如何修复:AttributeError:Class对象没有属性
我正在设置一个类,第一步是使用uu init u函数初始化属性。但是,当我尝试从该类创建实例时,它会显示AttributeError 我一次又一次地检查代码,看看语法是否有问题,但错误仍然存在Python 如何修复:AttributeError:Class对象没有属性,python,attributeerror,Python,Attributeerror,我正在设置一个类,第一步是使用uu init u函数初始化属性。但是,当我尝试从该类创建实例时,它会显示AttributeError 我一次又一次地检查代码,看看语法是否有问题,但错误仍然存在 class RandomWalk(): def ___init___(self, points = 10): """initialize attributes of a walk""" self.points = points self.x_valu
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作为点的默认值,但错误显示:
Traceback (most recent call last):
File "test1.py", line 10, in <module>
print(rw.points)
AttributeError: 'RandomWalk' object has no attribute 'points'
如果我用x_值或y_值替换属性点,则问题仍然存在,因为构造函数中有额外的下划线,因此不会调用它。两边各用两个:uu init_uu.构造函数中有额外的下划线,因此不会调用它。两边各用两个:uu init_uu.非常感谢,本!已经盯着密码看了好几个小时了。。。按照您的建议进行了修复,效果良好!谢谢你,本!已经盯着密码看了好几个小时了。。。按照您的建议进行了修复,效果良好!看看这个看看这个