Python Object-Oriented Python Inheritance Super-Duper!

Joshua Howard
Joshua Howard
10,512 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?

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