簡體   English   中英

如何修復:AttributeError:Class對象沒有屬性

[英]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_valuesy_values替換屬性points ,問題仍然存在

您的構造函數中有額外的下划線,因此不會被調用。 在兩側使用兩個: __init__

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM