473,750 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance error: class Foo has no attribute "bar"

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.attri b_dict["intel"]
*adding Character.__ini t__(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_dic t".

I can't see why it's saying this because Character.__ini t__(self) not only
has self.attrib_dic t = {} 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
5 3674
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.attri b_dict["intel"]
*adding Character.__ini t__(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_dic t".

I can't see why it's saying this because Character.__ini t__(self) not only
has self.attrib_dic t = {} 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(Characte r):
def __init__(self):
Character.__ini t__(self)
# do some stuff here..

?
Peace,
~Simon

Jul 9 '06 #2
Il Sun, 09 Jul 2006 04:24:01 GMT, crystalattice ha scritto:
I can't see why it's saying this because Character.__ini t__(self) not only
has self.attrib_dic t = {} 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
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_dic t = {}

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_at tribs.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
On Sun, 09 Jul 2006 03:51:56 -1000, Steven D'Aprano
<st***@REMOVETH IScyber.com.auw rote:
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_dic t = {}

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_at tribs.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
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\jackso cl\Python_stuff \CMRPG\CharCrea tion.py", line 262,
in __init__
self.intel = Character.attri b_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.attr ib_dict" to "self.attrib_di ct".

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_dic t = {}

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_at tribs.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6096
by: Danny Anderson | last post by:
Hola! I am working on a program where I am including a library that came with my numerical methods textbook. The "util.h" simply includes a large number of files. I had to change the util.h slightly to adjust path names and also take into account I am working with a case-sensitive OS. My program is below. The sticky point is that adding (#include "util.h") seems to negate the (#include <string>) statement somehow. How can I get...
3
1865
by: Roy | last post by:
Anyone have any links and/or code samples demonstrating how this can be done? Current procedure is that john doe clicks an item on a datagrid of mine and after however long, gets the info he wants. What I want to do is have a page that pops up "in-between" the two pages. Something that says "Processing Request: Please Wait" or somesuch and have a progress bar or something as well (would not have to be an accurate progress bar, per se)....
2
2485
by: Daniel | last post by:
how to make sure a xsl document has valid xsl syntax? i tried loading it into an xml document but that doesnt show syntax errors inside attributes such as "foo/bar" vs "bar\foo"
1
15958
by: Mary Ann | last post by:
I would like to write code in the On Open property of a form that hides the main menu bar toolbar. I have already customized the menu bar to all for showing/hiding but have no clue how to write the code in the form. Is it even possible to do this? If so, how? Thanks!
7
2087
by: adelfino | last post by:
I mean: unsigned char foo = ""; is the same as: unsigned char foo; memset (foo, '\0', bar); ?
2
1114
by: John M | last post by:
Hi, You may have seen on some websites a type of "navigation bar" where the current page and all previous pages in the website hierarchy are displayed. Each page is listed as a link. For example: Home > Support > Newsgroups
5
2587
by: Daves | last post by:
Hi, I'm using a asp.net 2.0 website to send out emails to users, the amount of which can reach up to 1500 users. Obviously the code sending the emails has to let the client know the mails are being sent out and display some kind of progress indicator. How would you implement this? Back in old asp 3.0 days I had a blank page where code did a Response.Write(.."mail sent"...) for each mail sent, the layout had to be simple because the page...
7
1859
by: Joe | last post by:
usually slide bars move by "jumps" while scrolling which looks not nice. I came accress slide bar with price menu, ewnt through script/web source code, and could not find anything that would case that wanted effect :) here you go: http://www.shopping.hp.com/webapp/shopping/computer_can_series.do?storeName=computer_store&category=notebooks&a1=Display&v1=20.1&series_name=HDX_series&a1=Display&v1=20.1 click on the left >Recommended config...
0
9000
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6804
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4713
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.