Search code examples
pythonpython-class

Why am I getting an error in Python like Dog() takes no arguments?


Why am I getting an error in Python like Dog() takes no arguments?

class Dog:
    def _init_(self, name, age):
        self.name = name
        self.age = age
my_dog = Dog('Willie', 6)
print(f"my dog's name is {my_dog.name}.")
print(f"my dog is {my_dog.age} years old.")

Solution

  • It is __init__ not _init_, so change the following line:

        def _init_(self, name, age)
    

    To:

        def _init_(self, name, age):
    

    P.S. you need a colon at the end of the line.