I am trying to make a class with generic __init__ values, but have defaults for its subclasses, as so:
class Enemy:
def __init__(self, difficulty, power, MaxHP, magic, MaxMP, speed, name):
self.power = power + 2*difficulty
self.HP = self.MaxHP = MaxHP + 5*difficulty
self.magic = magic + 2* difficulty
self.MP = self.MaxMP = MaxMP + 5*difficulty
class Goblin(Enemy):
def __init_(self, difficulty = 1, power = 1, MaxHP = 5, magic = 1, MaxMP = 5, speed = 5, name = "Goblin"):
super(Goblin, self).__init__(self, power, MaxHP, magic, MaxMP, speed, name)
However, when I try to make a Goblin object without the full number of default values (like, I’ll just put in a value for difficulty), it tells me I need the full 8 arguments even though the rest are given default values. Is there any reason I can’t do that or am I doing something wrong here?
Because you called
super(Goblin, self).__init__(self, power, MaxHP, magic, MaxMP, speed, name)withoutdifficulty. You probably also want to inherit likeclass Enemy(object)to make sureEnemyis a new-style class if you’re on 2.x (which I guess you must be, considering the old way that you’ve usedsuper).Here’s a simpler example:
Outputs :
Edit:
Well if the following doesn’t work, perhaps you have old class definitions cached in your interpreter (try running it on a fresh interpreter).