Hi guys, I am a bit new to OOP in general, and I was just wondering if I am going around what I want to do in the right way. I am making a small game to try to learn OOP and in this game I have several classes for different creatures, I create objects from these classes and add them to lists in a class called "Active". Now I am trying to create an actively updating list of all the creatures existing.

Would this be the right way of going about doing that:

class active:
    players = []
    goblins = []
    trolls  = []
    humans  = []
    bears   = []
    creaturelist = []

    def updatecreaturelist(self):
        for x in self.players:
            self.creaturelist.append(x)
        for x in self.goblins:
            self.creaturelist.append(x)
        for x in self.trolls:
            self.creaturelist.append(x)
        for x in self.humansr:
            self.creaturelist.append(x)
        for x in self.bears:
            self.creaturelist.append(x)

Also, what exactly is the difference between a class attribute and an instance attribute?
Any help much appreciated.
Thanks,
Karl

Probably not the best thing to do, though it will apparently work.

Your second question indicates that you have a clue. Class attributes belong to the class itself, whereas instance attributes belong to an instance (of the class). So your lists are in the class: There is only one class "active". By accessing these lists through self, you are obscuring the facts. Legal, but in my opinion confusing.

I would prefer to put the empty lists into the class instance:

class Active(object):
    def __init__(self):
        super(Active,self).__init__()
        self.players = []
        self.goblins = []
        # etc
    def update_creature_list(self)
        self.creaturelist.extend(self.players)
        # etc

I think this is a better option. It allows you to have multiple "active" instances (perhaps to save for an "undo" option, or for some other reason) and it "just seems better to me".

A little basic stuff about Class/OOP stuff that can be good to now.
Remeber it`s not called a function but a method,when it belong to Class.

class Active(object):
    ''' Special method __init__() is called auto when you makeing a object(acts as Constructor)'''
    def __init__(self):
        self.players = []
        self.goblins = []

    def put_first(self, first):
        '''This look like a function,put when belong to a Class is called a method'''
        return self.goblins.insert(0, first)

    def __str__(self):
        '''Special method __str__  get used when we use print on a object'''
        return 'players: %s goblins: %s' % (self.players, self.goblins)

#Now let test out this Class in IDLE

Here we se two empty list because stuff under __init__ get called auto when we make an object

>>> a_object = Active() #Class instance,we are making a object
>>> print a_object #print find __str__,and print out object info
players: [] goblins: []
>>>

We put in a couple of goblins.

>>> a_object.goblins.append('Grunt')
>>> a_object.goblins.append('Killer')
>>> print a_object
players: [] goblins: ['Grunt', 'Killer']

Last we are using method put_first to place a goblin first in the list.

>>> a_object.put_first('Gotar')
>>> print a_object
players: [] goblins: ['Gotar', 'Grunt', 'Killer']
>>>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.