OOP & Design Patterns - Observer Pattern - Event System, Publish-Subscribe
Review the following thread-safe observer pattern snippet. Identify the line that causes unsubscribed observers to still receive event notifications.
import threading
class Subject:
def __init__(self):
self.lock = threading.Lock()
self.observers = {}
def subscribe(self, observer, event):
with self.lock:
self.observers.setdefault(event, set()).add(observer)
def unsubscribe(self, observer, event):
with self.lock:
self.observers[event].remove(observer)
def notify(self, event, message):
with self.lock:
observers = self.observers.get(event, set())
for obs in observers:
obs.update(event, message)
