mercredi 6 mars 2019

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

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

Aucun commentaire:

Enregistrer un commentaire