class - Python subclass uses parent variables in constructor -
so i'm working on text based rpg , have class item looks this:
class item: def __init__(self, name, value): self.name = name self.value = value
next have weapon class want have inherit item class, , in weapon constructor want take "name" , "value" variables. in head works this:
class weapon(item): def __init(self, name, value, damage): self.damage = damage
which know wrong, that's how think should work. i've looked bunch of threads on "super()" function, none of explanations seem doing this. that's possible or should make weapon class separate? also... when work... constructor call like?
you can use follows inside weapon
class:
class weapon(item): def __init__(self, name, value, damage): super(weapon, self).__init__(name, value) self.damage = damage
and sure use new-style classes, i.e. inherit object
:
class item(object): ...
Comments
Post a Comment