18 hours ago · Tech · 0 comments

With the new event modifier, I think we can eliminate the button_pressed event. But first … I do like the notion of calling the modifier caller_id, so I think I’ll do that renaming. PyCharm helpfully renames throughout all the implementations of the protocol. But next … I don’t like this magic one_shot flag appearing in the public argument list: class PubSub: def subscribe(self, event, caller_id, callback, one_shot=False): key = (event, caller_id) subscription = Subscription(callback, one_shot) try: self.subscriptions[key].append(subscription) except KeyError: self.subscriptions[key] = [subscription] def subscribe_once(self, event, caller_id, callback): self.subscribe(event, '', callback, one_shot=True) Let’s extract a private method: def subscribe(self, event, caller_id, callback, one_shot=False): subscription = Subscription(callback, one_shot) self._store_subscription(subscription, event, caller_id) def _store_subscription(self, subscription, event, caller_id): key = (event,…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.