Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何修复:AttributeError:Class对象没有属性_Python_Attributeerror - Fatal编程技术网

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

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

我正在设置一个类,第一步是使用uu init u函数初始化属性。但是,当我尝试从该类创建实例时,它会显示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作为点的默认值,但错误显示:

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.

非常感谢,本!已经盯着密码看了好几个小时了。。。按照您的建议进行了修复,效果良好!谢谢你,本!已经盯着密码看了好几个小时了。。。按照您的建议进行了修复,效果良好!看看这个看看这个