An observer that can never leave is a memory leak. The clean idiom: subscribe hands back an unsubscribe function, so the caller can cancel without needing a reference to the subject or the list.
subscribe(observer): register the observer and return a function that removes it; calling it more than once must be safeclass Subject:
def __init__(self):
self._observers = []
def subscribe(self, observer):
"""Register observer and return a function that unsubscribes it."""
# TODO: add the observer to the end of self._observers, then build a closure
# over that observer which drops it from the list only while it is still
# present, so a repeat call is a harmless no-op. That closure is what the
# caller gets back.
pass
def notify(self, *args):
for observer in self._observers:
observer(*args)
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
An observer that can never leave is a memory leak. The clean idiom: subscribe hands back an unsubscribe function, so the caller can cancel without needing a reference to the subject or the list.
subscribe(observer): register the observer and return a function that removes it; calling it more than once must be safePress Run Tests or ββ© to check your solution.