Home > Software engineering >  Error: init() missing 2 required positional arguments: 'iswalking' and 'eat'
Error: init() missing 2 required positional arguments: 'iswalking' and 'eat'

Time:04-28

class Mammals:
    def __init__(self, iswalking, eat):
        self.eat = "Eating Sausages"
        self.iswalking = True
        print("walk")

class Dog(Mammals):
    def __init__(self):
        super().__init__()
        self.name = "Spiky"

pet = Dog()
print(pet.name)
Traceback (most recent call last):
  File "C:\Users\lenovo\PycharmProjects\pythonProject1\venv\RequestDemo.py", line 12, in <module>
    pet = Dog()
  File "C:\Users\lenovo\PycharmProjects\pythonProject1\venv\RequestDemo.py", line 9, in __init__
    super().__init__()
TypeError: __init__() missing 2 required positional arguments: 'iswalking' and 'eat'

CodePudding user response:

class Mammals:
    def __init__(self, iswalking=True, eat="Eating Sausages"):
        self.eat = eat
        self.iswalking = iswalking
        print("walk")


class Dog(Mammals):
    def __init__(self):
        super().__init__()
        self.name = "Spiky"


pet = Dog()
print(pet.name)

CodePudding user response:

class Mammals:
    def __init__(self, iswalking, eat):
        self.eat = "Eating Sausages"
        self.iswalking = True
        print("walk")

class Dog(Mammals):
    def __init__(self):
        super().__init__("",True)
        self.name = "Spiky"

pet = Dog()
print(pet.name)

OR

class Mammals:
    def __init__(self, eat="Eating Sausages", iswalking=True):
        self.eat = iswalking
        self.iswalking = eat
        print("walk")

class Dog(Mammals):
    def __init__(self):
        super().__init__("",True)
        self.name = "Spiky"

pet = Dog()
print(pet.name)
  • Related