On Sun, Aug 26, 2018 at 6:10 PM Alan Gauld via Tutor <tutor@python.org> wrote: > > On 26/08/18 23:38, boB Stepp wrote: > > > class SolitaireGame(): > > def __init__(self, name): > > self.name = name > > > Say I go with the aforementioned game with 13 separate scores to keep > > track of. The names of these games might be "Two_Mastery", > > "Three_Mastery", ... , "Ace_Mastery". In principle I want 13 objects > > with each one keeping track of each of the above games. Then I might > > want to switch to "Spider_Solitaire", keep track of its score, then go > > to something else, then back to Mastery, etc. How on earth am I to > > generate unique identifiers for each of these SolitaireGame objects in > > a rational way, not knowing in advance moment to moment what type of > > solitaire game I might be playing? > > A dictionary of objects keyed by name?
So you are saying do something like: class SolitaireGame(): def __init__(self, name): self.name = name def describe_self(self): print("This game of solitaire is called", self.name, ".") game_objects = {} def make_new_game_object(name): global game_objects game_objects[name[ = SolitaireGame(name) make_new_game_object('Chinese Solitaire') make_new_game_object('Ace Mastery') make_new_game_object('King Mastery') make_new_game_object('Spider') If I run the above in the interactive interpreter: 3.6.6: game_objects {'Chinese Solitaire': <__main__.SolitaireGame object at 0x7f3991d5e400>, 'Ace Mastery': <__main__.SolitaireGame object at 0x7f3991d5e470>, 'King Mastery': <__main__.SolitaireGame object at 0x7f3991d5e438>, 'Spider': <__main__.SolitaireGame object at 0x7f3991d5e4e0>} 3.6.6: game_objects['Spider'].describe_self() This game of solitaire is called Spider. This would seem to work, though I would have to be very careful to not allow the user to create a new game with the same name (Now a key.) which would overwrite an already existing game object. > If using a GUI add the names to a drop down or listbox > to ease later selection. Ultimately I would add a GUI interface. > Does that work for you? If what I wrote above describes what you intend, then yes. > > between them at will. Of course the intent is to persistently store > > these objects on disk upon program closure. > > Maybe JSON for that? Or even a shelve database? I plan to keep this simple. I will use a ".cfg" file to store game configuration information and a ".csv" file to store the actual records of hands played. But I will have to be careful how I generate the base filenames to avoid duplicates and potential nasty user-generated names. Though this project is only meant for my use ... -- boB _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor