Search code examples
pythonpython-3.xclasssubclasssuperclass

Problems with subclasses, unexpected arguments and unsolved references


So I'm trying to make an endless text adventure game (using help from this site), but I'm having some problems with my classes:

class Item:
def __init__(self, name, desc, usable, value):
    self.name = name
    self.desc = desc
    self.usable = usable
    self.value = value

def __str__(self):

    return "{}\n=====\n{}\nValue: {}\n".format(self.name, self.desc, self.usable, self.value)


class Weapon(Item):

    def __init__(self, damage):
        self.damage = damage
        super().__init__(desc, name, usable, value)

    def __str__(self):
        return "{}\n=====\n{}\nValue: {}\nDamage: {}".format(self.name, self.damage, self.desc, self.usable, self.value)


class BrokenSword(Weapon):

    def __init__(self):
        super(Weapon, self).__init__(name="Broken Sword",
                                     desc="A sword that didn't resist time.",
                                     value=1,
                                     usable=0,
                                     damage=1)

PyCharm states that desc, name, usable and value in the Weapon class (inside super().__init__()) are unsolved references, and that they are unexpected arguments in the BrokenSword(Weapon) class. The code is very similar to the tutorial one, so what is wrong with it? Was the tutorial written in python 2.x? How can I fix my code?


Solution

  • The Weapon class doesn't know what those arguments are. You have to define them either locally or -god forbid- globally. So Weapon.__init__ should accept arguments:

    class Weapon(Item):
        def __init__(self, desc, name, usable, value, damage):
            super().__init__(
                desc=desc,
                name=name,
                usable=usable,
                value=value
            )
            self.damage = damage