Inheritance error: class Foo has no attribute "bar"

Simon Forman rogue_pedro at yahoo.com
Sun Jul 9 01:34:38 EDT 2006


crystalattice wrote:
> I've finally figured out the basics of OOP; I've created a basic character
> creation class for my game and it works reasonably well. Now that I'm
> trying to build a subclass that has methods to determine the rank of a
> character but I keep getting errors.
>
> I want to "redefine" some attributes from the base class so I can use them
> to help determine the rank. However, I get the error that my base class
> doesn't have the dictionary that I want to use. I've tried several things
> to correct it but they don't work (the base class is called "Character"
> and the subclass is called "Marine"):
>
> *explicitly naming the Marine's attribute self.intel =
> Character.attrib_dict["intel"]
> *adding Character.__init__(self) to the Marine's __init__(self) method
> *changing the self.intel attribute from referencing the Character's
> dictionary to self (based on the assumption that since Marine is a
> 		subset of Character, it should have it's own attrib_dict being created
>
> Nothing seems to work; I still get the error "class Character has no
> attribute "attrib_dict".
>
> I can't see why it's saying this because Character.__init__(self) not only
> has self.attrib_dict = {} but it also calls the setAttribute method
> explicitly for each attribute name. If I do a print out of the dictionary
> just for Character, the attributes are listed.
>

Without a sample of your code and the actual tracebacks you're getting
it is difficult to know what's going wrong.


Are you writing your class like this:

class Character:
    def __init__(self):
        # do some stuff here..

class Marine(Character):
    def __init__(self):
        Character.__init__(self)
        # do some stuff here..

?


Peace,
~Simon




More information about the Python-list mailing list