By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,538 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,538 IT Pros & Developers. It's quick & easy.

Inheritance error: class Foo has no attribute "bar"

P: n/a
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.

--
Python-based online RPG based on the Colonial Marines from "Aliens" -
http://www.colonialmarinesrpg.com

Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 9 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

Jul 9 '06 #2

P: n/a
Il Sun, 09 Jul 2006 04:24:01 GMT, crystalattice ha scritto:
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.
Are you sure attrib_dict is a class attribute? Aren't you defining it in
charachter's __init__ method, thus making it an instance attribute?
--
Alan Franzoni <al***************@gmail.com>
-
Togli .xyz dalla mia email per contattarmi.
Rremove .xyz from my address in order to contact me.
-
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E
-
Blog: http://laterradeglieroi.verdiperronchi.com
Jul 9 '06 #3

P: n/a
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.

Jul 9 '06 #4

P: n/a
On Sun, 09 Jul 2006 03:51:56 -1000, Steven D'Aprano
<st***@REMOVETHIScyber.com.auwrote:
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 solutionto
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.

Yes, I believe your right (I left my code at work so I can't verify
directly). I did initialize attrib_dict in the __init__method, not
outside it as a class member. I'll try your suggestion and see what
happens. Thanks.
--
Python-based online RPG in development based on the Colonial Marines from
"Aliens": http://www.colonialmarinesrpg.com

Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 12 '06 #5

P: n/a
I checked my code today and your suggestion did fix the problem. I
used your second idea of having the default class attributes with
individual instance attributes. I ran into one problem where I kept
getting the error

" File "\\user1\jacksocl\Python_stuff\CMRPG\CharCreation. py", line 262,
in __init__
self.intel = Character.attrib_dict["intel"]
AttributeError: class Character has no attribute 'attrib_dict'
Script terminated."

but I figured out it was because I was calling the Character class
expressly in the Marine subclass when I wanted to rename a dictionary
value. It was just a simple remedy of changing the called dictionary
from "Character.attrib_dict" to "self.attrib_dict".

It gets so confusing keeping track of all things OOP items, though it's
quite a bit worse learning it in C++.

Thanks for your help.
Steven D'Aprano wrote:
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.
Jul 12 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.