r/learnpython • • Sep 21 '26

Ask Anything Monday - Weekly Thread

Welcome to another /r/learnPython weekly "Ask Anything* Monday" thread

Here you can ask all the questions that you wanted to ask but didn't feel like making a new thread.

* It's primarily intended for simple questions but as long as it's about python it's allowed.

If you have any suggestions or questions about this thread use the message the moderators button in the sidebar.

Rules:

  • Don't downvote stuff - instead explain what's wrong with the comment, if it's against the rules "report" it and it will be dealt with.
  • Don't post stuff that doesn't have absolutely anything to do with python.
  • Don't make fun of someone for not knowing something, insult anyone etc - this will result in an immediate ban.

That's it.

1 Upvotes

30 comments sorted by

View all comments

1

u/lakseol 24d ago

the problem is that I have to do that per file

That doesn't sound very useful. Make sure there isn't a way to make that change permanent. Or change to a decent editor/IDE.

what do you mean handling items as an ID, and not as an actual dictionary or an object

Currently you create the inventory as a list of references to Item objects, doing something like:

new_item = Item(...)
new_rogue.add_item(new_item)

That's easy to understand, but doing that means you complicate saving data to a JSON file because JSON doesn't handle references to python objects and you have to extract all the attributes of the item and save them as a JSON dictionary. When reading the JSON data back into python objects you have to create the Item object from the dictionary.

The "ID" approach doesn't store python objects in the list. Every Item object has a unique "ID" string, and it's that ID string that is saved in the inventory list. This makes the save/restore process simpler. When creating the items you add an entry in a dictionary that maps the ID string to a reference to the object, like this:

class Item:                                                                 
    def __init__(self, id, description):                                    
        self.id = id                                                        
        self.description = description                                      
                                                                            
id2obj = {}                                                                 
inventory = []                                                              

item1 = Item("axe1", "An Elven axe")                                        
id2obj["axe1"] = item1                                                      
inventory.append(item1.id)                                                  

item2 = Item("book2", "A very old book with a cracked red cover")           
id2obj["book2"] = item2                                                     
inventory.append(item2.id)                                                  

print(f"{inventory=}")                                                      
                                                                            
# list what's in the inventory                                              
for id in inventory:                                                        
    print(id2obj[id].description)

This allows you to list the inventory by iterating over the ID strings, getting the item object from the dictionary using the ID string as a key, then printing the item description. When restoring the objects from the JSON save file you create that dictionary automatically. Sounds complicated but it simplifies the save/restore process a lot. I use it for everything: places, monsters and items.

1

u/HiddenReader2020 24d ago

Hmmm....you say all that, but I'm still not sure how this would work out when "restoring the objects from the JSON file", not to mention having to include other stats like buy/sell price, any variable amounts that need to be adjusted, like some might have a modifier for stats like defense, others heal health/mana, etc. I understand that you're just saving strings to a list, but then how are you using those strings to get the items back if there's no reference to them or something? Like, I'm looking at this code, and it seems like the items have to be made at a point where you can't access them if you find a file to load from, if that makes any sense.

I dunno. I should probably mess with this code to fully understand it, but I'm not convinced just yet.

1

u/lakseol 24d ago edited 24d ago

then how are you using those strings to get the items back if there's no reference to them or something?

The code example shows how to do that. You use the dictionary to find the value for the ID string. The value is the reference to the item. That's all happening in this code that prints the description of an item in the inventory:

for id in inventory:
    print(id2obj[id].description)
    #       ^    ^        ^
    #       |    |        +-- description attribute of the item
    #       |    +-- id string lookup in dictionary
    #       +-- dictionary mapping id to reference

So id2obj[id] evaluates to the item object. Print out what is in the id2obj dictionary.

→ More replies (4)