Podobne pytania na temat SO to: this one i this. Przeczytałem również całą dokumentację online, którą mogę znaleźć, ale wciąż jestem zakłopotany. Byłbym wdzięczny za twoją pomoc.Python Class Inheritance AttributeError - dlaczego? jak naprawić?
Chcę użyć atrybutu Wandtype klasy Wand w mojej klasie LUMus metody CastSpell. Ale ciągle pojawia się błąd "Obiekt AttributeError:" CastSpell "nie ma atrybutu" wandtype "."
Ten kod działa:
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()
Kod ten, o usiłowanie dziedziczenia, nie.
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()
Próbowałem przy użyciu metody super() bezskutecznie. Naprawdę doceniam twoją pomoc w zrozumieniu a) dlaczego dziedziczenie klas nie działa w tym przypadku, b) jak zmusić go do działania.
Czy obiekt "CastSpell" naprawdę * może być obiektem "Różdżka"? – Darthfett
Po prostu chciałem uzyskać atrybut .wandtype, dlatego użyłem tego. Wiem, że to trochę dziwne. – user1186742
Dlaczego nie ma klasy 'Spell' z metodą" cast ", która po prostu przyjmuje argument różdżki jako argument? – Darthfett