Hi. I have the terrible problem that I never finish my projects because i get bored or distracted with other stuff.

So, I wanted to make something really easy: text-game.
Its about greek warriors in the olympics, so I need to assign a large number of attributes to each warrior. I want to do that through a function and I first thought about a dictionary.

This is what I have:

def CreateWarrior(name):
    attributes = {
        #ID
        "name": name,
        "age": 18,
        #Developed
        "strength": 0,
        ##Wrestling
        "reflexes": 0,
        "speed": 0,
        "intelligence": 0,
        "discipline": 0,
        "physcondition": 0,
        #Body
        "fat": 0,
        "muscle": 0,
        "hunger": 0,
        "sleepiness": 0,
        "health": 0,
        "damage": 0,
        #Psychological
        "mood": 0, # 0 = ultrahappy, 1 = happy, 2 = normal, 3 = mad, 4 = ultramad
        "moral": 0
    }

Now, I want to make this function to create a variable of the warrior's name, and assign that object all the attributes listed.

How can i do it?

Ok... Now I feel like I'm "talking" alone.
I found that you can create a newclass from a name with the method:

from new import classobj

classobj('name',(tuple),dictionary)
# I already have the name and dictionary parameters (the dictionary is all i wanted), but i don't have nor need (i think) a tuple

Do I really need a tuple? In what could I use it?

I'd do this:

def CreateWarrior(name):
    def __init__( self ):
        self.attributes = {
            #ID
            "name": name,
            "age": 18,
            #Developed
            "strength": 0,
            ##Wrestling
            "reflexes": 0,
            "speed": 0,
            "intelligence": 0,
            "discipline": 0,
            "physcondition": 0,
            #Body
            "fat": 0,
            "muscle": 0,
            "hunger": 0,
            "sleepiness": 0,
            "health": 0,
            "damage": 0,
            #Psychological
            "mood": 0, # 0 = ultrahappy, 1 = happy, 2 = normal, 3 = mad, 4 = ultramad
            "moral": 0
        }

The __init__ function is just what gets called when you create an instance of that class. And 'self' is a variable referring to the class instance, so that if you assign variables to self (self.attributes, etc), then they can be accessed from other functions within that class.

Also, inheritance is a wonderful thing, and creating one superclass for things and subclassing it for more specific things can prove very useful.

Yeah +1 to tomtetlaw. Classes are definitely the way to go. They provide a great way to put all your functions together as well as data that needs to interact can and will.

If you need to have a peek at classes then have a look here
http://www.python.org/doc/2.5.2/tut/node11.html
B

But also remember about new style classes. Now we use

class Warrior(object):
    #code

That is rather than without the "(object)". This means it has more attributes and is the standard way of doing things now.

Also above i think tomtetlaw made an error when he went

def CreateWarrior(name):
    def __init__( self ):
        self.attributes = {
            #ID
            "name": name,
            "age": 18,
            #Developed
            "strength": 0,
            ##Wrestling
            "reflexes": 0,
            "speed": 0,
            "intelligence": 0,
            "discipline": 0,
            "physcondition": 0,
            #Body
            "fat": 0,
            "muscle": 0,
            "hunger": 0,
            "sleepiness": 0,
            "health": 0,
            "damage": 0,
            #Psychological
            "mood": 0, # 0 = ultrahappy, 1 = happy, 2 = normal, 3 = mad, 4 = ultramad
            "moral": 0
        }

There are a few things wrong, first name should be in the init argument and he used the word def rather then class so instead what is should look like is:

class Warrior(object):
    def __init__( self,name ):
        self.attributes = {
            #ID
            "name": name,
            "age": 18,
            #Developed
            "strength": 0,
            ##Wrestling
            "reflexes": 0,
            "speed": 0,
            "intelligence": 0,
            "discipline": 0,
            "physcondition": 0,
            #Body
            "fat": 0,
            "muscle": 0,
            "hunger": 0,
            "sleepiness": 0,
            "health": 0,
            "damage": 0,
            #Psychological
            "mood": 0, # 0 = ultrahappy, 1 = happy, 2 = normal, 3 = mad, 4 = ultramad
            "moral": 0
        }

Then you just have to instantiate it and your on your way!

Hope that all helps.

Wow! That's embarrassing! My mistake, I accidentally copied out the 'def CreateWarrior' rather than changing it to 'class' :$
Sorry about that confusion, I'll proof-read my posts a second time from now on :P
P.S. I can't edit my above post now, so it's gonna stay the way it is for now...

Just wondering shadwickman, you know why the name has to be in the __init__ function parameters dont you? Just wondering seeing name is in your def thing, but hey, that might just be cause you completely forgot about classes :P

the name has to be in the __init__ function because it passes the paramaters to the class like so:

class MyClass:
    def __init__(name, age):
        self.name = name #the class now has an name attribute given to it from __init__
        self.age = age 

# so then i can go:
a_class = MyClass("Tom", 15) # <-- not my real age!

correct me if im wrong about anything ;D

No thats correct, thats the way i had it in my example. But you are very correct :) Apart from the fact that you have not got self as a argument in __init__.

self should be the first one

This article has been dead for over six months. Start a new discussion instead.