How to make a method from an abstract super class perform always the same first line of code in python?

久未见 提交于 2019-12-25 00:09:22

问题


I've implemented an abstract class BaseMicroservice with the following methods:

@abstractmethod
def handle(self, message):
    pass

def init_producer(self):
    self.producer = KafkaProducer(bootstrap_servers=self.config.get('kafka').get('bootstrap_servers'),
                                  value_serializer=lambda m: json.dumps(m).encode('utf-8'))
    self.is_prod_init = True

whenever handle is called in one of the sub classes that implement this method, I want the first thing to be done to be init_producer. What's the best way to achieve this? Repeating self.init_producer() in every handle in every class doesn't seem like a good workaround

来源:https://stackoverflow.com/questions/55025518/how-to-make-a-method-from-an-abstract-super-class-perform-always-the-same-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!