I'm just messing around with class inheritance. I originally had a separate goal in mind but I ended up here, and my new goal was to have a parent that was completely ignorant of it's self, i.e. no default attributes. This would seem to make it easier to subclass, the MeleClass
could set; for instance canRange
to True
during instantiation without modifying the parent's parameters.
Questions:
- Are there any other ways to do this?
- How could this be bad?
- Are there more efficient ways?
class Character:
def __init__(self, **kwargs):
for i in kwargs:
setattr(self, i, kwargs[i])
class MeleClass(Character):
def __init__(self, name="default", atk=1, deff=1):
self.canRange = False
Character.__init__(self, name=name, attack=atk, defence=deff)
class SpellClass(Character):
def __init__(self, name="default", atk=1, deff=1):
self.canRange = True
Character.__init__(self, name=name, attack=atk, defence=deff)
p = MeleClass(name='TheLazyScripter')
print p.name
print p.attack
print p.canRange
p = SpellClass()
leads toTypeError: unbound method __init__() must be called with Character instance as first argument (got nothing instead)
. Please correct that first. \$\endgroup\$