[Tutor] How to have unique identifiers for multiple object instances of a given class?

Alan Gauld alan.gauld at yahoo.co.uk
Sun Aug 26 19:09:10 EDT 2018


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?

If using a GUI add the names to a drop down or listbox
to ease later selection.

Does that work for you?

> 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?


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list