I recently found myself using empty Python classes as unique "tags" for an observer system.
For example, something like this...
from collections import defaultdict
class OnCreate:
pass
class OnModify:
pass
class OnDelete:
pass
class ObserverSystem:
def __init__(self):
self.observers = defaultdict(list)
def register(self, event, callback):
self.observers[event].append(callback)
def notify(self, event, *args, **kwargs):
for callback in self.observers[event]:
callback(*args, **kwargs)
observer = ObserverSystem()
observer.register(OnCreate, lambda entity: print(f"Entity {entity} created"))
observer.register(OnModify, lambda entity: print(f"Entity {entity} modified"))
observer.register(OnDelete, lambda entity: print(f"Entity {entity} deleted"))
observer.notify(OnCreate, 1)
observer.notify(OnModify, 2)
observer.notify(OnDelete, 3)
I guess, I could have used a of numeric value, or even a string as the "tag", but the class is unique, it's hashable so I can use it as dict key, etc. Seems to work well.