class Game:
def __init__(self, name):
self.name = name
self.callbacks = []
def start(self):
for callback in self.callbacks:
callback(self.name)
class Statistic:
startsCount = 0
lastGame = ""
def game_started(self, game_name):
self.lastGame = game_name
self.startsCount += 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.lastGame is "Heroes"
# statistic.startsCount is 2
print(statistic.lastGame)
print(statistic.startsCount)