Class initialization failing

2019-09-14 19:27发布

问题:

# Class and Instance Variables
class Dog:
    kind = 'canine'

    def __int__(self, name):
        self.name = name
        self.tricks = []

d = Dog('Fido')
e = Dog('Buddy')

print(d.kind)
print(e.kind)
print(d.name)
print(e.name)

Error report:

Traceback (most recent call last):
  File "dog.py", line 13, in <module>
    d = Dog('Fido')
TypeError: object() takes no parameters

回答1:

You have a typo. The method __int__ should be called __init__.