1

Trying to understand multiple inheritance.

I have created a parent class called 'Human' and from it created a child class called 'Man'. This child class inherits all attributes and methods except that the human_gender attribute is overwritten and set to 'male'. - this all works fine and as expected.

What I then tried to do it created a new child class called 'BoyChild' (from 'Man') which I hoped would inherit all of man's attributes and methods except that I wished to overwrite the age attribute to set the age to 8. This is throwing up an error.

Why is this error occurring? If I remove 'age=8' from the super().init parentheses, it inherits as normal, but I can't seem to overwrite the inherited class' attribute 'age'.

class human():
    '''This is a human class'''
    def __init__(self, human_gender = "unknown", age = 0,
                    hunger_level=0):
        self.human_gender = human_gender
        self.age = age
        self.hunger_level = hunger_level

    def setGender(self):
        self.human_gender = input("Please enter human's gender:")

    def setAge(self):
        self.age = int(input("Please enter human's age"))

    def setHunger_level(self):
        self.hunger_level = int(input("Please enter human's hunger level (0-10)"))

class man(human):
    '''This is a Man class'''
    def __init__(self):
        super().__init__(human_gender="male")

class boychild(man):
    '''This is a Boychild class'''
    def __init__(self):
        super().__init__(age=8)


guy = boychild()
#guy.setGender()
#guy.setAge()
guy.setHunger_level()
print("The human is a: ", guy.human_gender)
print("The human is: ", guy.age)
print("The human's hunger level is: ", guy.hunger_level)

input()

2 Answers 2

4

The __init__ method of man doesn't accept keyword arguments at all. But since you rely on it to work, you should use them:

class man(human):
    '''This is a Man class'''
    def __init__(self, **kwargs):
        super().__init__(human_gender="male", **kwargs)

class boychild(man):
    '''This is a Boychild class'''
    def __init__(self, **kwargs):
        super().__init__(age=8, **kwargs)

This way you catch every argument not handled by your class and just passt it on.

Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant! Very clear and helpful. Exactly what I needed. Thank you!
3

The class boychild inherits the class man. Therefore, when you call super().__init__(age=8), it refers to man.__init__, that have no arguments.

You could do something like this:

class man(human):
    '''This is a Man class'''
    def __init__(self, age = 0, hunger_level=0):
        super().__init__(human_gender="male", age, hunger_level)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.