views:

97

answers:

2

I have a Club class and a Player Class. The player class has an attribute Fav.clubs which will have unique club values. So the user is supposed to enter various club names. Based on the club names I must retrieve those club objects and establish the relationship that this particular player has this Fav.clubs.

The attribute Fav.clubs in Player class should store the names of Club. Now what I have to do is, take input from user about Fav.clubs (a list). After that traverse each element in the list and access the string name to find the corresponding club object and then store that object instance in Player class.

A: 

Use a dict to store a map between strings and instances of Club:

clubmap = {
  'basketball': Club("Basketball club"),
  'chess': Club("Chess club"),
}

someplayer.joinclub(clubmap['basketball'])
Ignacio Vazquez-Abrams
The problem is I'm making UI.So in player class I'm asking the user to enter fav.clubs.I dunno how to make dictionary using raw_input,but know how to make a list.so I made a list and now I check club class for has_key....Now I want to retrieve this object and store this object in player class in either a list or a dict
gizgok
+1  A: 

Store all clubs in a dictionary called all_clubs. The key should be the club-name and the value the club object itself. Then you can do all_clubs[clubname] to retrieve the club object for a given name.

The player might have an attribute club_names which is the list of the unique names you described and a property clubs which might look like this:

class Player(object):
    # ...

    @property
    def clubs(self):
        result = []
        for name in self.club_names:
            result.append(all_clubs.get(name))
        return result

Alternatively, it might also be a good idea to use a ORM tool like sqlalchemy and a simple file-based or in-memory sqlite database. Then you have the power of SQL and a extremely good relational mapping. But if you are new to Python, I wouldn't use something like that, because sqlalchemy is quite a complex topic and the mapping uses some python magic in the background, which you might not understand at the beginning. Therefore, I would suggest the first method.

tux21b
The problem is I'm making UI.So in player class I'm asking the user to enter fav.clubs.I dunno how to make dictionary using raw_input,but know how to make a list.so I made a list and now I check club class for has_key....Now I want to retrieve this objectand store this object in player class in either a list or a dict
gizgok
The method I've explained requires you to call in the UI method for adding new clubs where you ask the user to enter the club name (and all other details like location?, leader?, etc) `all_clubs[clubname] = club` after the club object has been created.For the player itself, you only have to enter the value for club_names, which should be a list. You might use something like `raw_input().split(',')` to generate this list from the user input.The connection of the two classes is done in the property. `Player.clubs` is a list containing all Club objects which are named in `Player.club_names`.
tux21b