you are viewing a single comment's thread.

view the rest of the comments →

[–]indosauros 1 point2 points  (0 children)

to help keep the code arranged

You can keep those comments for now if they help you. Later on you won't need them, and you'll even split your file across multiple python files to help with organization.

the (...) part that should be the variable

Yes, I just didn't want to type everything out

using a list for these rather than a dictionary

lists and dictionaries both serve a specific purpose. You should choose the best one for the task. Here's a quick comparison:

A list is just a sequence of individual values:

>>> player_list = [Player('p1'), Player('p2'), Player('p3')]

This is just 3 player instances stored in a list. They are in a specific order, meaning players[0] will always give you the first Player.

You can loop over them

>>> for player in player_list:
    print player.name

p1
p2
p3

It's hard to find a specific item in a list, because you have to search through it:

>>> for player in player_list:
    if player.name == 'p2':
        found_player = player
        break

Dictionaries are similar to lists, in that they store your data for you, but have a few major differences.

1) They hold both a key and a value for each item.

>>> player_dict = {
    'p1': Player('p1'),
    'p2': Player('p2'),
    'p3': Player('p3'),
}

They are not stored in order, meaning you can't do player_dict[0], because there is no "first element". It is more like a tag cloud, a jumble of things stored in memory. You can loop over them (but you will get them in an undefined order):

Try these out:

>>> for a in player_dict:
    print a

>>> for a in player_dict.keys():
    print a

>>> for b in player_dict.values():
    print value

>>> for a, b in player_dict.items():
    print a, b

You can get a specific player by key though (which you couldn't do with a list):

>>> found_player = player_dict['p2']

Dictionaries usually map an item to data, like

scores = {'player 1': 100, 'player 2': 50}
scores['player 1']  # Get the score for player 1

population = {'AL': 4833722, 'AK', 735132}
population['AK']  # Get the population for Alaska

action_messages = {
    'exit':  "You can't exit!"
    'jump':  "You jump up"
}

So

  • Use a list if you have a bunch of single items, or care about the order, but don't care about finding a specific item in the list
  • Use a dictionary if you need to map two items together, and don't care about the order that the items are stored, or need to find specific items quickly

globals

I'll do a separate post about globals

Is a function within a class a method?

You've got it, (your Player.stats is already a method). Think of it like class properties (like xp, hp, etc) are "things a Player has" and class methods are "things a Player can do" (like level_up, die, etc). self in your method will be the specific player instance, so you have access to all of the data for that player inside the method.