Python Object-Oriented Python Inheritance Multiple Superclasses

Artur Owczarek
Artur Owczarek
4,781 Points

Why Kenneth in the beginning of class initiates attribute?

The question as in the title. This is my files of attributes, thieves, character:

import random

class Sneaky:

    def __init__(self, sneaky=True, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.sneaky = sneaky

    def hide(self, light_level):
        return self.sneaky and light_level < 10
class Agile:

    def __init__(self, agile=True, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.agile = agile

    def evade(self):
        return self.agile and random.randint(0, 1)
import random

from characters import Character
from attributes import Agile, Sneaky

class Thief(Sneaky, Agile, Character):
    def pickpocket(self):
        return self.sneaky and bool(random.randint(0, 1))
class Character:

    def __init__(self, name='', **kwargs): = name

        for key, value in kwargs.items():
            setattr(self, key, value)

Sorry for format, but I can't see options for formatting. Anyway, I didn't it what Kenneth did and it works all fine. I mean he writes agile = True in the beginning Agile class, I don't know why.

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 59,027 Points

Good Question. You are correct that, since the class instances set the attributes during instance initialization, they do not also need attributes to be set in the class.

One situation that would warrant setting in both the class and in __init__ is if a classmethod relied on the attribute. When running classmethods there is no instance and hence no self available.