Preguntas similares en SO incluyen: this one y this. También he leído toda la documentación en línea que puedo encontrar, pero todavía estoy bastante confundido. Estaría agradecido por tu ayuda.Herencia de clase de Python AttributeError - ¿por qué? ¿como arreglar?
Quiero usar el atributo .wandtype de la clase Wand en mi método Castuspell clase lumus. Pero sigo obteniendo el error "AttributeError: el objeto 'CastSpell' no tiene ningún 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, de intento de herencia, no.
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()
He intentado utilizar el método super() en vano. Realmente apreciaría su ayuda para entender a) por qué la herencia de clase no funciona en este caso, b) cómo hacer que funcione.
¿Debería un objeto 'CastSpell' realmente * ser * un objeto 'Varita'? – Darthfett
Simplemente quería obtener el atributo .wandtype, que es la razón por la que lo he usado. Suena un poco raro, lo sé. – user1186742
¿Por qué no tener una clase 'Spell' con un método' cast', que simplemente toma el tipo de varita como argumento? – Darthfett