Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Inheritance Super-Duper!

I don't know why, but this is confusing to me?

Whelp, made it this far before I became totally confused on a what I am doing with this super(). My main point of confusion is why I need to use it. Is the sole purpose so that I can take methods of the Super class and modify them slightly?

So when i'm using the Super() method, do I only use this when I want to modify an inherited piece of code?

Hi Joshua. Yes, I believe you're correct.

By using super() when we initialize a subclass (e.g. a Thief or a Knight as subclasses of Characters), we reduce written code. After all, all characters have the ordinary character attributes, like a name, etc. Maybe even a hometown, a race (e.g. Elf, Dwarf, Human, Vulcan), so those can be additional parameters to the Character class. However, not all characters have a sneaky attribute, nor an attribute for magic spells, or Vulcan mind meld success rate.

Yes to your second question, super() I've always seen in this form:

class Character():
    def __init__(self, name, **kwargs):
        self.name = name
        for key, value in kwargs.items():
            setattr(self, key, value)

class Archer():
    long_range_attack = True

    def __init__(self, name, long_range_attack=True, **kwargs):
        super().__init__(name, **kwargs)

I hope that makes sense :-)