简体   繁体   English

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

[英]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_valuesy_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.

相关问题 如何修复:“AttributeError:&#39;Class&#39; 对象没有属性 &#39;rect&#39;” - How to fix: "AttributeError: 'Class' object has no attribute 'rect'" AttributeError: 'class' object 没有属性 - AttributeError: 'class' object has no attribute 如何修复 AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;click&#39; - How to fix AttributeError: 'NoneType' object has no attribute 'click' 如何修复“AttributeError: &#39;str&#39; object has no attribute &#39;content&#39;”python 错误 - how to fix for the "AttributeError: 'str' object has no attribute 'content' "python error 如何修复AttributeError:&#39;NoneType&#39;对象没有属性&#39;theme_cls&#39; - How to fix AttributeError: 'NoneType' object has no attribute 'theme_cls' 如何修复 AttributeError: &#39;bytes&#39; object has no attribute &#39;encode&#39;? - How do I fix AttributeError: 'bytes' object has no attribute 'encode'? 如何修复AttributeError:&#39;HTMLParserTreeBuilder&#39;对象没有属性&#39;initialize_soup&#39; - how to fix AttributeError: 'HTMLParserTreeBuilder' object has no attribute 'initialize_soup' 如何修复“”AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;app&#39; - How to fix ""AttributeError: 'NoneType' object has no attribute 'app' AttributeError: &#39;int&#39; 对象没有属性 &#39;get&#39; 我该如何修复它 - AttributeError: 'int' object has no attribute 'get' how can i fix it 如何修复 AttributeError:ResultSet 对象没有属性“get_text” - How to fix AttributeError: ResultSet object has no attribute 'get_text'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM