Атрибут Наследования классов Python - почему? как исправить?
Подобные вопросы по SO включают в себя:этот а такжеэтот, Я также прочитал всю онлайновую документацию, которую смог найти, но я все еще в замешательстве. Я был бы благодарен за вашу помощь.
Я хочу использовать атрибут Wand класса .wandtype в моем методе класса CastSpell lumus. Но я продолжаю получать сообщение об ошибке "AttributeError:" CastSpell & apos; У объекта нет атрибута "wandtype". & quot;
Этот код работает:
class Wand(object):
def __init__(self, wandtype, length):
self.length = length
self.wandtype = wandtype
def fulldesc(self):
print "This is a %s wand and it is a %s long" % (self.wandtype, self.length)
class CastSpell(object):
def __init__(self, spell, thing):
self.spell = spell
self.thing = thing
def lumus(self):
print "You cast the spell %s with your wand at %s" %(self.spell, self.thing)
def wingardium_leviosa(self):
print "You cast the levitation spell."
my_wand = Wand('Phoenix-feather', '12 inches')
cast_spell = CastSpell('lumus', 'door')
my_wand.fulldesc()
cast_spell.lumus()
Этот код с попыткой наследования не соответствует.
class Wand(object):
def __init__(self, wandtype, length):
self.length = length
self.wandtype = wandtype
def fulldesc(self):
print "This is a %s wand and it is a %s long" % (self.wandtype, self.length)
class CastSpell(Wand):
def __init__(self, spell, thing):
self.spell = spell
self.thing = thing
def lumus(self):
print "You cast the spell %s with your %s wand at %s" %(self.spell, self.wandtype, self.thing) #This line causes the AttributeError!
print "The room lights up."
def wingardium_leviosa(self):
print "You cast the levitation spell."
my_wand = Wand('Phoenix-feather', '12 inches')
cast_spell = CastSpell('lumus', 'door')
my_wand.fulldesc()
cast_spell.lumus()
Я пытался использовать метод super (), но безрезультатно. Я очень ценю вашу помощь в понимании а) почему наследование классов не работает в этом случае, б) как заставить его работать.