Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7960511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:46:29+00:00 2026-06-04T04:46:29+00:00

Similar questions on SO include: this one and this . I’ve also read through

  • 0

Similar questions on SO include: this one and this. I’ve also read through all the online documentation I can find, but I’m still quite confused. I’d be grateful for your help.

I want to use the Wand class .wandtype attribute in my CastSpell class lumus method. But I keep getting the error “AttributeError: ‘CastSpell’ object has no attribute ‘wandtype’.”

This code works:

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() 

This code, with attempted inheritance, doesn’t.

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() 

I’ve tried using the super() method to no avail. I’d really appreciate your help understanding a) why class inheritance isn’t working in this case, b) how to get it to work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:46:31+00:00Added an answer on June 4, 2026 at 4:46 am

    To put it simply, you override Wand.__init__ in the class that inherits from it, so CastSpell.wandtype is never set in CastSpell. Besides that, my_wand can’t pass information into cast_spell, so you’re confused about the role of inheritance.

    Regardless of how you do it, you have to somehow pass length and wandtype to CastSpell. One way would be to include them directly into CastSpell.__init__:

    class CastSpell(Wand):
        def __init__(self, spell, thing, length, wandtype):
            self.spell = spell 
            self.thing = thing
            self.length = length
            self.wandtype = wandtype
    

    Another, more generic way would be to pass these two to the base class’ own __init__():

    class CastSpell(Wand):
        def __init__(self, spell, thing, length, wandtype):
            self.spell = spell 
            self.thing = thing
            super(CastSpell, self).__init__(length, wandtype)
    

    Another way would be to stop making CastSpell inherit from Wand (is CastSpell a kind of Wand? or something a Wand does?) and instead make each Wand be able to have some CastSpells in it: instead of “is-a” (a CastSpell is a kind of Wand), try “has-a” (a Wand has Spells).

    Here’s a simple, not so great way to have a Wand store spells:

    class Wand(object):
        def __init__(self, wandtype, length):
            self.length = length
            self.wandtype = wandtype
            self.spells = {} # Our container for spells. 
            # You can add directly too: my_wand.spells['accio'] = Spell("aguamenti", "fire")
    
        def fulldesc(self):
            print "This is a %s wand and it is a %s long" % (self.wandtype, self.length)
    
        def addspell(self, spell):
            self.spells[spell.name] = spell
    
        def cast(self, spellname):
            """Check if requested spell exists, then call its "cast" method if it does."""
            if spellname in self.spells: # Check existence by name
                spell = self.spells[spellname] # Retrieve spell that was added before, name it "spell"
                spell.cast(self.wandtype) # Call that spell's cast method, passing wandtype as argument
            else:
                print "This wand doesn't have the %s spell." % spellname
                print "Available spells:"
                print "\n".join(sorted(self.spells.keys()))
    
    
    class Spell(object):
        def __init__(self, name, target):
            self.name = name
            self.target = target
    
        def cast(self, wandtype=""):
            print "You cast the spell %s with your %s wand at %s." % (
                   self.name, wandtype, self.target)
            if self.name == "lumus":
                print "The room lights up."
            elif self.name == "wingardium leviosa":
                print "You cast the levitation spell.",
                print "The %s starts to float!" % self.target
    
        def __repr__(self):
            return self.name
    
    my_wand = Wand('Phoenix-feather', '12 inches')
    lumus = Spell('lumus', 'door')
    wingardium = Spell("wingardium leviosa", "enemy")
    
    my_wand.fulldesc()
    lumus.cast() # Not from a Wand! I.e., we're calling Spell.cast directly
    print "\n\n"
    
    my_wand.addspell(lumus) # Same as my_wand.spells["lumus"] = lumus
    my_wand.addspell(wingardium)
    print "\n\n"
    
    my_wand.cast("lumus") # Same as my_wand.spells["lumus"].cast(my_wand.wandtype)
    print "\n\n"
    my_wand.cast("wingardium leviosa")
    print "\n\n"
    my_wand.cast("avada kadavra") # The check in Wand.cast fails, print spell list instead
    print "\n\n"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize there are similar questions on this topic, but I still cannot find
I've seen other questions very similar to this but somehow I still can't get
similar questions have been asked before but I cant find an exact match to
Similar questions have been asked but cannot find a solution that works well. What
There are similar questions to this, but I don't think anyone has asked this
I searched SO and found similar questions, but none compared all three. That surprised
I have asked a similar question to this one already but I think it
this question is very similar to this one but my case is a bit
I've seen some similar questions on stack but I don't think this is a
Ok, I know similar questions have been asked, but my groveling through Google searches

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.