Skip to content Skip to sidebar Skip to footer

Dynamically Decorate A Function Inside A Class In Python3

This is an extension of Dynamic/runtime method creation (code generation) in Python @John Montgommery properly answered @Eli Bendersky 's question that you can dynamically create a

Solution 1:

You can directly apply the decorator when creating the method:

def add_sequence(cls, order, path):
    @seq_task(order)
    def sequence(self):
        self.client.get(path)
    sequence.__name__ = "sequence%d" % order
    setattr(cls, sequence.__name__, sequence)

Post a Comment for "Dynamically Decorate A Function Inside A Class In Python3"