Re: python class with in a class

Hi,
I have a question that is mainly just curiosity.  When importing my
classes, I get an error if I do:
import character
It says something like module is not callable.
However, if I do:
from character import character
Then it works as expected.  My question is, what's the difference?  I
thought that you only needed to use from if you only wanted to import
part of a class file?
Also, is there a way to save and retreive simple .txt settings files?
Something like:
currentLevel=1
I did lots of googling and didn't come up with anything, so I thought I
would ask before trying to role my own.
Thanks for the help
PS  Oh, one last thing, does anyone have any experience with pygame?
On Thu, 2009-04-09 at 10:15 -0400, Jamal Mazrui wrote:

> Yes, here is an example of that approach.
> 
> Jamal
> 
> # soldier.py
> 
> class weapon():
>     def __init__(self, name='short-sword', damage_type='cut',
> damage_level=5):
>         self.name = name
>         self.damage_type = damage_type
>         self.damage_level = damage_level
> 
> class armor():
>     def __init__(self, name='leather', ac=2):
>         self.name = name
>         self.ac = ac
> 
> class soldier():
>     def __init__(self, name='marine', strength=5, speed=5,
> armor=armor(), weapon=weapon()):
>         self.name = name
>         self.strength = strength
>         self.speed = speed
>         self.armor = armor
>         self.weapon = weapon
> 
> if __name__ == '__main__':
>     character = soldier()
>     print 'character.name =', character.name
>     print 'character.armor.name =', character.armor.name
>     print 'character.weapon.name =', character.weapon.name
> 
> """ Program output
> character.name = marine
> character.armor.name = leather
> character.weapon.name = short-sword
> """
> 
> On Wed, 8
> Apr
> 2009, Tyler
> Littlefield wrote:
> 
> > Date: Wed, 8 Apr 2009 23:43:59 -0600
> > From: Tyler Littlefield <tyler@xxxxxxxxxxxxx>
> > Reply-To: programmingblind@xxxxxxxxxxxxx
> > To: programmingblind@xxxxxxxxxxxxx
> > Subject: Re: python class with in a class
> >
> > make class soldier have default armor and weapon as properties.
> >
> >
> > Thanks,
> > Tyler Littlefield
> > Web: tysdomain.com
> > email: tyler@xxxxxxxxxxxxx
> > My programs don't have bugs, they're called randomly added features.
> >
> >   ----- Original Message -----
> >   From: BlueScale
> >   To: programmingblind@xxxxxxxxxxxxx
> >   Sent: Wednesday, April 08, 2009 9:46 PM
> >   Subject: python class with in a class
> >
> >
> >   Hi,
> >   Classes have never been my strong point.  But here's my question.  If I 
> > have a class soldier, and a class weapon, and a class armor, how do I 
> > combind them in to one character.  For example, the default weapon will be 
> > a shortsword with a set damage, damage type, etc.  The default armor type 
> > is leather with an ac of something like 2.  the class soldier sets 
> > attributes about the person like speed, strength, quickness, etc.  so how 
> > do I make the character class wield the default weapon and have the default 
> > armor?  Or, am I going about this all wrong?  Should weapons and armor be 
> > part of the soldier class?
> >   Thanks
> __________
> View the list's information and change your settings at 
> http://www.freelists.org/list/programmingblind
> 

Other related posts: