Create random, unique variable names for objects
Question: I'm playing around with Python and any programming language for the first
time, so please bear with me. I started an online class two weeks ago, but try
to develop a small game at the side to learn faster (and have fun). It's a
text adventure, but is shall have random encounters and fights with enemies
that have random equipment that the players can then loot.

This is my problem: If I create random objects/weapons for my random
encounter, I need to make sure that the object has a unique name. The way the
level is designed there could in theory be an infinite number of objects (it
can be open ended, with encounters just popping up).

This is my approach so far

    
    
    class Item_NPCs: #create objects for NPCs
    
        def__init__(self, item_type, item_number):
        # e.g. item_type 1 = weapons, item_type2 = potions, etc.
    
            if item_type == 1 and item number == 1:
                 self.property1 = 5
                 self.property2 = 4
    
            if item_type == 1 and item_number ==2:
    
           # etc. :)
    
    def prepare_encounter(): 
    
        inventory_NPC = [] # empty list for stuff the NPC carries around
    
        XXX = Class_Item(item_type, item_number) # I might randomize the arguments.   
    

What is important is that I want "XXX" to be unique and random, so that no
object exists more than once and can later be put into the player's inventory.

How to do that?

Joe


Answer: Why do you need it to be random ? You could simply use a list, and append
every new object to the list, with its index being its unique identifier :

    
    
    items = []
    items.append( Class_Item(item_type, item_number) )
    

But if you really need random identifier, maybe you can use a dictionary :

    
    
    items = dict()
    items[random_id] = Class_Item(item_type, item_number)
    

This requires random_id to be hashable (but it should be if it is a number or
a string).

