Re: Inheritance error: class Foo has no attribute "bar"



On Sun, 09 Jul 2006 04:24:01 +0000, 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"):

Without seeing your class definitions, it is hard to tell what you are
doing, but I'm going to take a guess: you're defining attributes in the
instance instead of the class.

E.g.

class Character():
def __init__(self):
self.attrib_dict = {}

attrib_dict is now an instance attribute. Every instance will have one,
but the class doesn't.

I'm thinking you probably want something like this:

class Character():
attrib_dict = {}
def __init__(self):
pass

Now attrib_dict is an attribute of the class. However, it also means that
all instances will share the same values! Here's one possible solution to
that:

class Character():
default_attribs = {}
def __init__(self):
self.attribs = self.default_attribs.copy()

Now there is one copy of default character attributes, shared by the class
and all it's instances, plus each instance has it's own unique set of
values which can be modified without affecting the defaults.


Hope this clears things up for you.


--
Steven.

.



Relevant Pages

  • Re: Inheritance error: class Foo has no attribute "bar"
    ... class Character has no attribute 'attrib_dict' ... expressly in the Marine subclass when I wanted to rename a dictionary ... I want to "redefine" some attributes from the base class so I can use them ... Without seeing your class definitions, it is hard to tell what you are ...
    (comp.lang.python)
  • Re: Inheritance error: class Foo has no attribute "bar"
    ... 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. ... and the subclass is called "Marine"): ...
    (comp.lang.python)
  • Re: Inheritance error: class Foo has no attribute "bar"
    ... 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. ... and the subclass is called "Marine"): ...
    (comp.lang.python)
  • Re: double message box again
    ... > is operator for [distinguishing character types]? ... Suppose you create an abstract base class along ... which means that a subclass can replace it with its own ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: MustInherit in Window forms
    ... and it'll be run if the SUBCLASS implementor chooses not to implement ... much depends on whether your base class is usable itself: ... > DisconnectFromAbrServer() method there, and it'll be run if the base class ... > implementor chooses not to implement a custom version of the method. ...
    (microsoft.public.dotnet.languages.vb)