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 Super-Duper!

Joshua Howard
Joshua Howard
12,798 Points

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?

1 Answer

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 :-)