I'm currently experiencing an issue when trying to use an object as a key. I wanted to use the user at index 0 as key for the game as index 0, but when I do this all game objects are updated with the new user item. Here is the code i'm using:
class User():
def __init__(self, user_id, age, name):
self.user_id = user_id
self.age = age
self.name = name
class Game():
score = {}
def __init__(self, difficulty):
self.difficulty = difficulty
# users
users_list = [User(1, 'Jake', 19), User(2, 'George', 32), User(3, 'Harry', 27)]
# games
game_list = [Game('Easy'), Game('Intermediate'), Game('Hard')]
# Give user in index 0 score of 1000 for easy game
game_list[0].score[users_list[0]] = 1000
print(game_list[0].score)
print(game_list[1].score)
print(game_list[2].score)
Here is the output after running the above code:
{<__main__.User object at 0x7fc9b6cdd4c0>: 1000}
{<__main__.User object at 0x7fc9b6cdd4c0>: 1000}
{<__main__.User object at 0x7fc9b6cdd4c0>: 1000}
I should have probably made a separate score class I suppose, but anyhow I would love to know what is the reason behind this. Any help and advice is much appreciated.