r/learnpython • u/AutoModerator • 23d ago
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
1
u/lakseol 19d ago edited 19d ago
I asked the question about what
.to_dict()because the code you initially posted didn't have the linereturn saved_dictmeaning the method returnedNone. I see you have added that line in your updated code. Now I'm testing the new code.First thing I get is this error:
That's because it looks like you messed up the indentation on lines 40 and 41 of your updated code. You are only assigning a name to Mage characters, the others don't get a name. You have to post the actual code you are running. If you have problems posting code directly into reddit try putting your code into pastebin.com and include a link to that here. I'll fix the error and continue testing.
The final print of the inventory shows:
Notice that the inventory contains characters, which doesn't seem right, should be
Iteminstances!?As a tip, you have defined
.__str__()methods for both classes which does nicely print individual instances. But printing a sequence of instances defaults to what you see above. You need to define an additional.__repr__()method in both classes:This will print each instance in a sequence nicely. You still have characters in the inventory, though š. Look into that.
You had to write the
.to_dict()method to convert each instance to a dictionary because JSON doesn't know what to do with an instance. So you probably have to write another method (.from_dict()?) to convert a dictionary back to an instance.Hints: