Python 中的实例化

Instantisation in Python

我刚开始在 Python 学习编程。我发现很难正确实例化对象。它给出了追溯,但我不知道为什么。我一直在阅读 3.8 的文档,但仍然不确定是什么导致了错误?非常感谢您的帮助。

谢谢。

    def _init_(self, name, age, character):
        self.name = name
        self.age = age
        self.character = character

    def date_of_birth():
        return 2020 - self.age



Breeds = Breeds = [Dog("Alsation", 2,["Protective","Smart"]), Dog("Rotteweiler", 3,["Possessive","Aggressive"]), Dog("Chihuahua",1,["Loud, Jumpy"])]

sum = 0
for dog in Breeds:
    sum = sum + Dog.age

print("The average age of breeds is: " + str(sum/len(Breeds)))'''


```Traceback (most recent call last):
  File "C:\Users\Hilary\Desktop\hello.py", line 12, in <module>
    Breeds = Breeds = [Dog("Alsation", 2,["Protective","Smart"]), Dog("Rotteweiler", 3,["Possessive","Aggressive"]), Dog("Chihuahua",1,["Loud, Jumpy"])]
TypeError: Dog() takes no arguments
>>> ```

__init__方法要求左右2个下划线,你用的是1个。

因此无法识别您的 init 方法,并且 Python 没有看到任何采用您的参数数量的构造函数。