Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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):
        self.name = 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.

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 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.

I'm taking the oop course again to understand more. Can you please explain how can we use super() in parent class in attributes.py. I thought super() is used only in the subclass to call parent class init method. I tried to run the code in Jupyter notebook, I get en error which says that 'init' takes one exactly argument which makes sense.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

super() can be used within any class method to call the first method of the same name found in the listed parent classes along the MRO.

If super() is found again in the parent class, the next method of the same name found in the MRO is run. This will continue until no more super() calls are found.

Be sure you know which __init__ the error refers to. The stacktrace will show the line number.

Post back if you need more help. Good luck!!!