Python Class Inheritance AttributeError - dlaczego? jak naprawić?
Podobne pytania dotyczące SO obejmują:ten ito. Przeczytałem również całą dokumentację online, którą mogę znaleźć, ale nadal jestem dość zdezorientowany. Byłbym wdzięczny za twoją pomoc.
Chcę użyć atrybutu .wandtype klasy Wand w metodzie lumus klasy CastSpell. Ale ciągle otrzymuję błąd „AttributeError: obiekt„ 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()
Ten kod, z próbą dziedziczenia, nie 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(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 użyć metody super () bezskutecznie. Byłbym wdzięczny za pomoc w zrozumieniu a) dlaczego dziedziczenie klas nie działa w tym przypadku, b) jak go uruchomić.