class Game:
def __init__(self, name):
self.name = name
self.callbacks = []
def start(self):
for callback in self.callbacks:
callback(self.name)
class Statistic:
starts_count = 0
last_game = ""
def game_started(self, game_name):
self.last_game = game_name
self.starts_count += 1
statistic = Statistic()
heroes = Game("Heroes")
doom = Game("Doom")
# subscribe to events
heroes.callbacks.append(statistic.game_started)
doom.callbacks.append(statistic.game_started)
doom.start()
heroes.start()
# statistic.last_game is "Heroes"
# statistic.starts_count is 2
print(statistic.last_game)
print(statistic.starts_count)