AtributoErro da Herança de Classe Python - por quê? como consertar?
Perguntas semelhantes sobre o SO incluem:este eisto. Também li toda a documentação on-line que posso encontrar, mas ainda estou bastante confusa. Eu ficaria grato pela sua ajuda.
Desejo usar o atributo wandtype da classe Wand no método lumus da minha classe CastSpell. Mas eu continuo recebendo o erro "AttributeError: 'CastSpell' objeto não tem atributo 'wandtype'."
Este código funciona:
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()
Este código, com tentativa de herança, não.
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()
Eu tentei usar o método super () sem sucesso. Eu realmente aprecio a sua ajuda para entender a) por que a herança de classes não está funcionando neste caso, b) como fazê-lo funcionar.